
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>基础 · 千锋大前端小册-GP19-Node.js</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="古艺散人">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="01-Node.js基础.html" />
    
    
    <link rel="prev" href="../" />
    

    
        <link rel="shortcut icon" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="bookmark" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../source/images/apple-touch-icon.jpg'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../source/images/apple-touch-icon.jpg">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../source/images/apple-touch-icon.jpg">
        
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.2.1" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    01-Node.js基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02-yarn.html">
            
                <a href="02-yarn.html">
            
                    
                    02-Yarn入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="03-Express.html">
            
                <a href="03-Express.html">
            
                    
                    03-Express
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04-Koa2.html">
            
                <a href="04-Koa2.html">
            
                    
                    04-Koa2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="05-MongoDB.html">
            
                <a href="05-MongoDB.html">
            
                    
                    05-MongoDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06-socket.html">
            
                <a href="06-socket.html">
            
                    
                    06-socket编程
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    高级
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    01-Node中间件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../advanced/02-Node事件循环.html">
            
                <a href="../advanced/02-Node事件循环.html">
            
                    
                    02-Node事件循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../advanced/03-Node中间层.html">
            
                <a href="../advanced/03-Node中间层.html">
            
                    
                    03-Node中间层
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../thanks/">
            
                <a href="../thanks/">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >基础</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;nodejs&#x662F;&#x4EC0;&#x4E48;"><b></b>&#x4E00;&#x3001;Node.js&#x662F;&#x4EC0;&#x4E48;</a></li><ul><ul><li><span class="title-icon "></span><a href="#1&#x3001;&#x7279;&#x6027;"><b></b>1&#x3001;&#x7279;&#x6027;</a></li><li><span class="title-icon "></span><a href="#2&#x3001;&#x4E3E;&#x4F8B;"><b></b>2&#x3001;&#x4E3E;&#x4F8B;</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;node-&#x76F8;&#x5173;&#x5DE5;&#x5177;"><b></b>&#x4E8C;&#x3001;Node &#x76F8;&#x5173;&#x5DE5;&#x5177;</a></li><ul><ul><li><span class="title-icon "></span><a href="#1&#x3001;nvm-node-version-manager"><b></b>1&#x3001;NVM: Node Version Manager</a></li><li><span class="title-icon "></span><a href="#2&#x3001;npm-node-package-manager"><b></b>2&#x3001;NPM: Node Package Manager</a></li><li><span class="title-icon "></span><a href="#3&#x3001;nrm-npm-registry-manager"><b></b>3&#x3001;NRM: npm registry manager</a></li><li><span class="title-icon "></span><a href="#4&#x3001;npx-npm-package-extention"><b></b>4&#x3001;NPX: npm package extention</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;&#x6A21;&#x5757;&#x5305;-&#x4E0E;-commonjs"><b></b>&#x4E09;&#x3001;&#x6A21;&#x5757;/&#x5305; &#x4E0E; CommonJS</a></li><ul><ul><li><span class="title-icon "></span><a href="#1&#x3001;&#x6A21;&#x5757;&#x5305;&#x5206;&#x7C7B;"><b></b>1&#x3001;&#x6A21;&#x5757;/&#x5305;&#x5206;&#x7C7B;</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;"><b></b>&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;</a></li><ul><ul><li><span class="title-icon "></span><a href="#1&#x3001;url"><b></b>1&#x3001;url</a></li><li><span class="title-icon "></span><a href="#2&#x3001;querystring"><b></b>2&#x3001;querystring</a></li><li><span class="title-icon "></span><a href="#3&#x3001;httphttps"><b></b>3&#x3001;http/https</a></li><li><span class="title-icon "></span><a href="#4&#x3001;events"><b></b>4&#x3001;Events</a></li><li><span class="title-icon "></span><a href="#5&#x3001;file-system"><b></b>5&#x3001;File System</a></li><li><span class="title-icon "></span><a href="#6&#x3001;stream"><b></b>6&#x3001;Stream</a></li><li><span class="title-icon "></span><a href="#7&#x3001;zlib"><b></b>7&#x3001;Zlib</a></li><li><span class="title-icon "></span><a href="#8&#x3001;readline"><b></b>8&#x3001;ReadLine</a></li><li><span class="title-icon "></span><a href="#9&#x3001;crypto"><b></b>9&#x3001;Crypto</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;&#x8DEF;&#x7531;"><b></b>&#x56DB;&#x3001;&#x8DEF;&#x7531;</a></li><li><span class="title-icon "></span><a href="#&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;"><b></b>&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;</a></li><ul><ul><li><span class="title-icon "></span><a href="#51-readstaticfile"><b></b>5.1 readStaticFile</a></li><li><span class="title-icon "></span><a href="#52-server"><b></b>5.2 server</a></li><li><span class="title-icon "></span><a href="#53-&#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><b></b>5.3 &#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;</a></li></ul></ul></ul></div><a href="#&#x4E00;&#x3001;nodejs&#x662F;&#x4EC0;&#x4E48;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x4E00;&#x3001;nodejs&#x662F;&#x4EC0;&#x4E48;"><a name="&#x4E00;&#x3001;nodejs&#x662F;&#x4EC0;&#x4E48;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;nodejs&#x662F;&#x4EC0;&#x4E48;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;&#x3001;nodejs&#x662F;&#x4EC0;&#x4E48;" class="plugin-anchor" href="#&#x4E00;&#x3001;nodejs&#x662F;&#x4EC0;&#x4E48;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;Node.js&#x662F;&#x4EC0;&#x4E48;</h1>
<p>Node.js&#xAE; is a JavaScript runtime built on Chrome&apos;s V8 JavaScript engine.</p>
<h3 id="1&#x3001;&#x7279;&#x6027;"><a name="1&#x3001;&#x7279;&#x6027;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x7279;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x7279;&#x6027;" class="plugin-anchor" href="#1&#x3001;&#x7279;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x7279;&#x6027;</h3>
<p>Node.js &#x53EF;&#x4EE5;&#x89E3;&#x6790;JS&#x4EE3;&#x7801;&#xFF08;&#x6CA1;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7EA7;&#x522B;&#x7684;&#x9650;&#x5236;&#xFF09;&#x63D0;&#x4F9B;&#x5F88;&#x591A;&#x7CFB;&#x7EDF;&#x7EA7;&#x522B;&#x7684;API&#xFF0C;&#x5982;&#xFF1A;</p>
<ul>
<li>&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199; (File System)</li>
<li>&#x8FDB;&#x7A0B;&#x7684;&#x7BA1;&#x7406; (Process)</li>
<li>&#x7F51;&#x7EDC;&#x901A;&#x4FE1; (HTTP/HTTPS)</li>
<li>&#x2026;&#x2026;</li>
</ul>
<h3 id="2&#x3001;&#x4E3E;&#x4F8B;"><a name="2&#x3001;&#x4E3E;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x4E3E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x4E3E;&#x4F8B;" class="plugin-anchor" href="#2&#x3001;&#x4E3E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x4E3E;&#x4F8B;</h3>
<h4 id="21-&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7EA7;&#x522B;&#x7684;&#x9650;&#x5236;"><a name="21-&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7EA7;&#x522B;&#x7684;&#x9650;&#x5236;" class="anchor-navigation-ex-anchor" href="#21-&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7EA7;&#x522B;&#x7684;&#x9650;&#x5236;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7EA7;&#x522B;&#x7684;&#x9650;&#x5236;" class="plugin-anchor" href="#21-&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7EA7;&#x522B;&#x7684;&#x9650;&#x5236;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7EA7;&#x522B;&#x7684;&#x9650;&#x5236;</h4>
<p><strong>Ajax&#x6D4B;&#x8BD5;</strong></p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>browser-safe-sandbox<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>browser-safe-sandbox<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="actionscript">
    <span class="hljs-keyword">const</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest()
    xhr.open(<span class="hljs-string">&apos;get&apos;</span>, <span class="hljs-string">&apos;https://m.maoyan.com/ajax/moreClassicList?sortId=1&amp;showType=3&amp;limit=10&amp;offset=30&amp;optimus_uuid=A5518FF0AFEC11EAAB158D7AB0D05BBBD74C9789D9F649898982E6542C7DD479&amp;optimus_risk_level=71&amp;optimus_code=10&apos;</span>, <span class="hljs-literal">false</span>)
    xhr.send()
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><strong>&#x6D4F;&#x89C8;&#x5668;&#x9884;&#x89C8;</strong></p>
<pre><code>browser-sync start --server --files **/* --directory
</code></pre><h4 id="22-&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;-file-system"><a name="22-&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;-file-system" class="anchor-navigation-ex-anchor" href="#22-&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;-file-system"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;-file-system" class="plugin-anchor" href="#22-&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;-file-system"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 &#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199; (File System)</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)

fs.readFile(<span class="hljs-string">&apos;./ajax.png&apos;</span>, <span class="hljs-string">&apos;utf-8&apos;</span>, (err, content) =&gt; {
  <span class="hljs-built_in">console</span>.log(content)
})
</code></pre>
<h4 id="23-&#x8FDB;&#x7A0B;&#x7684;&#x7BA1;&#x7406;&#xFF08;process&#xFF09;"><a name="23-&#x8FDB;&#x7A0B;&#x7684;&#x7BA1;&#x7406;&#xFF08;process&#xFF09;" class="anchor-navigation-ex-anchor" href="#23-&#x8FDB;&#x7A0B;&#x7684;&#x7BA1;&#x7406;&#xFF08;process&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="23-&#x8FDB;&#x7A0B;&#x7684;&#x7BA1;&#x7406;&#xFF08;process&#xFF09;" class="plugin-anchor" href="#23-&#x8FDB;&#x7A0B;&#x7684;&#x7BA1;&#x7406;&#xFF08;process&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>2.3 &#x8FDB;&#x7A0B;&#x7684;&#x7BA1;&#x7406;&#xFF08;Process&#xFF09;</h4>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params">argv</span>) </span>{
  <span class="hljs-built_in">console</span>.log(argv)
}

main(process.argv.slice(<span class="hljs-number">2</span>))
</code></pre>
<p><strong>&#x8FD0;&#x884C;</strong></p>
<pre><code>node 2.3-process.js argv1 argv2
</code></pre><h4 id="24-&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#xFF08;httphttps&#xFF09;"><a name="24-&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#xFF08;httphttps&#xFF09;" class="anchor-navigation-ex-anchor" href="#24-&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#xFF08;httphttps&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="24-&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#xFF08;httphttps&#xFF09;" class="plugin-anchor" href="#24-&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#xFF08;httphttps&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>2.4 &#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#xFF08;HTTP/HTTPS&#xFF09;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;http&quot;</span>)

http.createServer((req,res) =&gt; {
  res.writeHead(<span class="hljs-number">200</span>, {
    <span class="hljs-string">&quot;content-type&quot;</span>: <span class="hljs-string">&quot;text/plain&quot;</span>
  })
  res.write(<span class="hljs-string">&quot;hello nodejs&quot;</span>)
  res.end()
}).listen(<span class="hljs-number">3000</span>)
</code></pre>
<h1 id="&#x4E8C;&#x3001;node-&#x76F8;&#x5173;&#x5DE5;&#x5177;"><a name="&#x4E8C;&#x3001;node-&#x76F8;&#x5173;&#x5DE5;&#x5177;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;node-&#x76F8;&#x5173;&#x5DE5;&#x5177;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;&#x3001;node-&#x76F8;&#x5173;&#x5DE5;&#x5177;" class="plugin-anchor" href="#&#x4E8C;&#x3001;node-&#x76F8;&#x5173;&#x5DE5;&#x5177;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;Node &#x76F8;&#x5173;&#x5DE5;&#x5177;</h1>
<h3 id="1&#x3001;nvm-node-version-manager"><a name="1&#x3001;nvm-node-version-manager" class="anchor-navigation-ex-anchor" href="#1&#x3001;nvm-node-version-manager"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;nvm-node-version-manager" class="plugin-anchor" href="#1&#x3001;nvm-node-version-manager"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;NVM: Node Version Manager</h3>
<p><strong>1.1 Mac &#x5B89;&#x88C5; nvm</strong></p>
<pre><code>https://github.com/nvm-sh/nvm/blob/master/README.md
</code></pre><p><strong>1.2 Windows &#x5B89;&#x88C5; nvm</strong></p>
<pre><code>nvm-windows
nodist
</code></pre><h3 id="2&#x3001;npm-node-package-manager"><a name="2&#x3001;npm-node-package-manager" class="anchor-navigation-ex-anchor" href="#2&#x3001;npm-node-package-manager"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;npm-node-package-manager" class="plugin-anchor" href="#2&#x3001;npm-node-package-manager"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;NPM: Node Package Manager</h3>
<h4 id="21-&#x5168;&#x5C40;&#x5B89;&#x88C5;package"><a name="21-&#x5168;&#x5C40;&#x5B89;&#x88C5;package" class="anchor-navigation-ex-anchor" href="#21-&#x5168;&#x5C40;&#x5B89;&#x88C5;package"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-&#x5168;&#x5C40;&#x5B89;&#x88C5;package" class="plugin-anchor" href="#21-&#x5168;&#x5C40;&#x5B89;&#x88C5;package"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x5168;&#x5C40;&#x5B89;&#x88C5;package</h4>
<pre><code>$ npm install forever --global (-g)
$ forever
$ npm uninstall forever --global
$ forever
</code></pre><p><strong>&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x5305;&#x7684;&#x76EE;&#x5F55;</strong></p>
<ul>
<li>Mac<pre><code>/Users/felix/.nvm/versions/node/nvm&#x5404;&#x4E2A;&#x7248;&#x672C;/bin/
</code></pre></li>
<li>Windows<pre><code>C:\Users\&#x4F60;&#x7684;&#x7528;&#x6237;&#x540D;\AppData\Roaming\npm\node_modules
</code></pre></li>
</ul>
<h4 id="22-&#x672C;&#x5730;&#x5B89;&#x88C5;package"><a name="22-&#x672C;&#x5730;&#x5B89;&#x88C5;package" class="anchor-navigation-ex-anchor" href="#22-&#x672C;&#x5730;&#x5B89;&#x88C5;package"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-&#x672C;&#x5730;&#x5B89;&#x88C5;package" class="plugin-anchor" href="#22-&#x672C;&#x5730;&#x5B89;&#x88C5;package"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 &#x672C;&#x5730;&#x5B89;&#x88C5;package</h4>
<pre><code>$ cd ~/desktop
$ mkdir gp-project
$ cd gp-project
$ npm install underscore
$ npm list (ls)
</code></pre><h4 id="23-packagejson&#x521D;&#x59CB;&#x5316;"><a name="23-packagejson&#x521D;&#x59CB;&#x5316;" class="anchor-navigation-ex-anchor" href="#23-packagejson&#x521D;&#x59CB;&#x5316;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="23-packagejson&#x521D;&#x59CB;&#x5316;" class="plugin-anchor" href="#23-packagejson&#x521D;&#x59CB;&#x5316;"><i class="fa fa-link" aria-hidden="true"></i></a>2.3 package.json&#x521D;&#x59CB;&#x5316;</h4>
<pre><code>$ pwd
$ npm init -y
$ ls
$ cat package.json
</code></pre><h4 id="24-&#x4F7F;&#x7528;packagejson"><a name="24-&#x4F7F;&#x7528;packagejson" class="anchor-navigation-ex-anchor" href="#24-&#x4F7F;&#x7528;packagejson"><i class="fa fa-link" aria-hidden="true"></i></a><a name="24-&#x4F7F;&#x7528;packagejson" class="plugin-anchor" href="#24-&#x4F7F;&#x7528;packagejson"><i class="fa fa-link" aria-hidden="true"></i></a>2.4 &#x4F7F;&#x7528;package.json</h4>
<pre><code>$ npm install underscore --save
$ cat package.json
$ npm install lodash --save-dev
$ cat package.json
$ rm -rf node_modules
$ ls
$ npm install
$ npm uninstall underscore --save
$ npm list | grep underscore
$ cat package.json
</code></pre><h4 id="25-&#x5B89;&#x88C5;&#x6307;&#x5B9A;&#x7248;&#x672C;&#x7684;&#x5305;"><a name="25-&#x5B89;&#x88C5;&#x6307;&#x5B9A;&#x7248;&#x672C;&#x7684;&#x5305;" class="anchor-navigation-ex-anchor" href="#25-&#x5B89;&#x88C5;&#x6307;&#x5B9A;&#x7248;&#x672C;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="25-&#x5B89;&#x88C5;&#x6307;&#x5B9A;&#x7248;&#x672C;&#x7684;&#x5305;" class="plugin-anchor" href="#25-&#x5B89;&#x88C5;&#x6307;&#x5B9A;&#x7248;&#x672C;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.5 &#x5B89;&#x88C5;&#x6307;&#x5B9A;&#x7248;&#x672C;&#x7684;&#x5305;</h4>
<pre><code>$ pwd
$ npm list
$ npm info underscore
$ npm view underscore versions
$ npm install underscore@1.8.0
$ npm list
$ npm uninstall underscore
$ npm list
</code></pre><h4 id="26-&#x66F4;&#x65B0;&#x672C;&#x5730;&#x5B89;&#x88C5;&#x7684;&#x5305;"><a name="26-&#x66F4;&#x65B0;&#x672C;&#x5730;&#x5B89;&#x88C5;&#x7684;&#x5305;" class="anchor-navigation-ex-anchor" href="#26-&#x66F4;&#x65B0;&#x672C;&#x5730;&#x5B89;&#x88C5;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="26-&#x66F4;&#x65B0;&#x672C;&#x5730;&#x5B89;&#x88C5;&#x7684;&#x5305;" class="plugin-anchor" href="#26-&#x66F4;&#x65B0;&#x672C;&#x5730;&#x5B89;&#x88C5;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.6 &#x66F4;&#x65B0;&#x672C;&#x5730;&#x5B89;&#x88C5;&#x7684;&#x5305;</h4>
<pre><code>$ npm info underscore
$ npm view underscore versions
$ npm install underscore@1.4.4 --save-dev
$ npm list | grep gulp
$ npm outdated //~2.0.0&#x8868;&#x793A;patch, ^2.0.0&#x8868;&#x793A;minor * &#x8868;&#x793A;xx&#x6700;&#x65B0;&#x7248;&#x672C;
$ npm list | grep gulp
$ npm update
</code></pre><h4 id="27-&#x6E05;&#x9664;&#x7F13;&#x5B58;"><a name="27-&#x6E05;&#x9664;&#x7F13;&#x5B58;" class="anchor-navigation-ex-anchor" href="#27-&#x6E05;&#x9664;&#x7F13;&#x5B58;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="27-&#x6E05;&#x9664;&#x7F13;&#x5B58;" class="plugin-anchor" href="#27-&#x6E05;&#x9664;&#x7F13;&#x5B58;"><i class="fa fa-link" aria-hidden="true"></i></a>2.7 &#x6E05;&#x9664;&#x7F13;&#x5B58;</h4>
<pre><code>npm cache clean --force
</code></pre><h4 id="28-&#x4E0A;&#x4F20;&#x81EA;&#x5DF1;&#x7684;&#x5305;"><a name="28-&#x4E0A;&#x4F20;&#x81EA;&#x5DF1;&#x7684;&#x5305;" class="anchor-navigation-ex-anchor" href="#28-&#x4E0A;&#x4F20;&#x81EA;&#x5DF1;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="28-&#x4E0A;&#x4F20;&#x81EA;&#x5DF1;&#x7684;&#x5305;" class="plugin-anchor" href="#28-&#x4E0A;&#x4F20;&#x81EA;&#x5DF1;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8 &#x4E0A;&#x4F20;&#x81EA;&#x5DF1;&#x7684;&#x5305;</h4>
<h6 id="281-&#x7F16;&#x5199;&#x6A21;&#x5757;"><a name="281-&#x7F16;&#x5199;&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#281-&#x7F16;&#x5199;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="281-&#x7F16;&#x5199;&#x6A21;&#x5757;" class="plugin-anchor" href="#281-&#x7F16;&#x5199;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8.1 &#x7F16;&#x5199;&#x6A21;&#x5757;</h6>
<p>&#x4FDD;&#x5B58;&#x4E3A;index.js</p>
<pre><code class="lang-js">exports.sayHello = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{ 
  <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Hello World&apos;</span>; 
}
</code></pre>
<h6 id="282-&#x521D;&#x59CB;&#x5316;&#x5305;&#x63CF;&#x8FF0;&#x6587;&#x4EF6;"><a name="282-&#x521D;&#x59CB;&#x5316;&#x5305;&#x63CF;&#x8FF0;&#x6587;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#282-&#x521D;&#x59CB;&#x5316;&#x5305;&#x63CF;&#x8FF0;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="282-&#x521D;&#x59CB;&#x5316;&#x5305;&#x63CF;&#x8FF0;&#x6587;&#x4EF6;" class="plugin-anchor" href="#282-&#x521D;&#x59CB;&#x5316;&#x5305;&#x63CF;&#x8FF0;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8.2 &#x521D;&#x59CB;&#x5316;&#x5305;&#x63CF;&#x8FF0;&#x6587;&#x4EF6;</h6>
<p>$ npm init 
package.json</p>
<pre><code class="lang-json">{ 
  <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;gp19-npm&quot;</span>, 
  <span class="hljs-string">&quot;version&quot;</span>: <span class="hljs-string">&quot;1.0.1&quot;</span>, 
  <span class="hljs-string">&quot;description&quot;</span>: <span class="hljs-string">&quot;gp19 self module&quot;</span>, 
  <span class="hljs-string">&quot;main&quot;</span>: <span class="hljs-string">&quot;index.js&quot;</span>,
  <span class="hljs-string">&quot;scripts&quot;</span>: { 
    <span class="hljs-string">&quot;test&quot;</span>: <span class="hljs-string">&quot;make test&quot;</span> 
  }, 
  <span class="hljs-string">&quot;repository&quot;</span>: { 
    <span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;Git&quot;</span>, 
    <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;git+https://github.com/lurongtao/gp19-npm.git&quot;</span> 
  }, 
  <span class="hljs-string">&quot;keywords&quot;</span>: [ 
    <span class="hljs-string">&quot;demo&quot;</span> 
  ], 
  <span class="hljs-string">&quot;author&quot;</span>: <span class="hljs-string">&quot;Felixlu&quot;</span>, 
  <span class="hljs-string">&quot;license&quot;</span>: <span class="hljs-string">&quot;ISC&quot;</span>, 
  <span class="hljs-string">&quot;bugs&quot;</span>: { 
    <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;https://github.com/lurongtao/gp19-npm/issues&quot;</span> 
  }, 
  <span class="hljs-string">&quot;homepage&quot;</span>: <span class="hljs-string">&quot;https://github.com/lurongtao/gp19-npm#readme&quot;</span>, 
}
</code></pre>
<h6 id="283-&#x6CE8;&#x518C;npm&#x4ED3;&#x5E93;&#x8D26;&#x53F7;"><a name="283-&#x6CE8;&#x518C;npm&#x4ED3;&#x5E93;&#x8D26;&#x53F7;" class="anchor-navigation-ex-anchor" href="#283-&#x6CE8;&#x518C;npm&#x4ED3;&#x5E93;&#x8D26;&#x53F7;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="283-&#x6CE8;&#x518C;npm&#x4ED3;&#x5E93;&#x8D26;&#x53F7;" class="plugin-anchor" href="#283-&#x6CE8;&#x518C;npm&#x4ED3;&#x5E93;&#x8D26;&#x53F7;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8.3 &#x6CE8;&#x518C;npm&#x4ED3;&#x5E93;&#x8D26;&#x53F7;</h6>
<pre><code>https://www.npmjs.com &#x4E0A;&#x9762;&#x7684;&#x8D26;&#x53F7;
felix_lurt/qqmko09ijn
$ npm adduser
</code></pre><h6 id="284-&#x4E0A;&#x4F20;&#x5305;"><a name="284-&#x4E0A;&#x4F20;&#x5305;" class="anchor-navigation-ex-anchor" href="#284-&#x4E0A;&#x4F20;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="284-&#x4E0A;&#x4F20;&#x5305;" class="plugin-anchor" href="#284-&#x4E0A;&#x4F20;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8.4 &#x4E0A;&#x4F20;&#x5305;</h6>
<pre><code>$ npm publish
</code></pre><p>&#x5751;&#xFF1A;403 Forbidden</p>
<pre><code>&#x67E5;&#x770B;npm&#x6E90;&#xFF1A;npm config get registry
&#x5207;&#x6362;npm&#x6E90;&#x65B9;&#x6CD5;&#x4E00;&#xFF1A;npm config set registry http://registry.npmjs.org
&#x5207;&#x6362;npm&#x6E90;&#x65B9;&#x6CD5;&#x4E8C;&#xFF1A;nrm use npm
</code></pre><h6 id="285-&#x5B89;&#x88C5;&#x5305;"><a name="285-&#x5B89;&#x88C5;&#x5305;" class="anchor-navigation-ex-anchor" href="#285-&#x5B89;&#x88C5;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="285-&#x5B89;&#x88C5;&#x5305;" class="plugin-anchor" href="#285-&#x5B89;&#x88C5;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8.5 &#x5B89;&#x88C5;&#x5305;</h6>
<pre><code>$ npm install gp19-npm
</code></pre><h6 id="286-&#x5378;&#x8F7D;&#x5305;"><a name="286-&#x5378;&#x8F7D;&#x5305;" class="anchor-navigation-ex-anchor" href="#286-&#x5378;&#x8F7D;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="286-&#x5378;&#x8F7D;&#x5305;" class="plugin-anchor" href="#286-&#x5378;&#x8F7D;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8.6 &#x5378;&#x8F7D;&#x5305;</h6>
<pre><code>&#x67E5;&#x770B;&#x5F53;&#x524D;&#x9879;&#x76EE;&#x5F15;&#x7528;&#x4E86;&#x54EA;&#x4E9B;&#x5305; &#xFF1A;
npm ls
&#x5378;&#x8F7D;&#x5305;&#xFF1A;
npm unpublish --force
</code></pre><h6 id="287-&#x4F7F;&#x7528;&#x5F15;&#x5165;&#x5305;"><a name="287-&#x4F7F;&#x7528;&#x5F15;&#x5165;&#x5305;" class="anchor-navigation-ex-anchor" href="#287-&#x4F7F;&#x7528;&#x5F15;&#x5165;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="287-&#x4F7F;&#x7528;&#x5F15;&#x5165;&#x5305;" class="plugin-anchor" href="#287-&#x4F7F;&#x7528;&#x5F15;&#x5165;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.8.7 &#x4F7F;&#x7528;&#x5F15;&#x5165;&#x5305;</h6>
<pre><code>var hello = require(&apos;gp19-npm&apos;)
hello.sayHello()
</code></pre><h4 id="29-npm-&#x811A;&#x672C;"><a name="29-npm-&#x811A;&#x672C;" class="anchor-navigation-ex-anchor" href="#29-npm-&#x811A;&#x672C;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="29-npm-&#x811A;&#x672C;" class="plugin-anchor" href="#29-npm-&#x811A;&#x672C;"><i class="fa fa-link" aria-hidden="true"></i></a>2.9 npm &#x811A;&#x672C;</h4>
<p>Node &#x5F00;&#x53D1;&#x79BB;&#x4E0D;&#x5F00; npm&#xFF0C;&#x800C;&#x811A;&#x672C;&#x529F;&#x80FD;&#x662F; npm &#x6700;&#x5F3A;&#x5927;&#x3001;&#x6700;&#x5E38;&#x7528;&#x7684;&#x529F;&#x80FD;&#x4E4B;&#x4E00;&#x3002;</p>
<p><strong>&#x4E00;&#x3001;&#x4EC0;&#x4E48;&#x662F; npm &#x811A;&#x672C;&#xFF1F;</strong></p>
<p>npm &#x5141;&#x8BB8;&#x5728; package.json &#x6587;&#x4EF6;&#x91CC;&#x9762;&#xFF0C;&#x4F7F;&#x7528; scripts &#x5B57;&#x6BB5;&#x5B9A;&#x4E49;&#x811A;&#x672C;&#x547D;&#x4EE4;&#x3002;</p>
<pre><code class="lang-json">{
  <span class="hljs-comment">// ...</span>
  <span class="hljs-string">&quot;scripts&quot;</span>: {
    <span class="hljs-string">&quot;build&quot;</span>: <span class="hljs-string">&quot;node build.js&quot;</span>
  }
}
</code></pre>
<p><strong>&#x4E8C;&#x3001;&#x6267;&#x884C;&#x987A;&#x5E8F;</strong></p>
<p>&#x5982;&#x679C; npm &#x811A;&#x672C;&#x91CC;&#x9762;&#x9700;&#x8981;&#x6267;&#x884C;&#x591A;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x90A3;&#x4E48;&#x9700;&#x8981;&#x660E;&#x786E;&#x5B83;&#x4EEC;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;&#x3002;</p>
<p>script1.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> x = <span class="hljs-number">0</span>
<span class="hljs-built_in">console</span>.log(x)
</code></pre>
<p>script2.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> y = <span class="hljs-number">0</span>
<span class="hljs-built_in">console</span>.log(y)
</code></pre>
<pre><code class="lang-json"><span class="hljs-string">&quot;scripts&quot;</span>: {
  <span class="hljs-string">&quot;script1&quot;</span>: <span class="hljs-string">&quot;node script1.js&quot;</span>,
  <span class="hljs-string">&quot;script2&quot;</span>: <span class="hljs-string">&quot;node script2.js&quot;</span>
}
</code></pre>
<p>&#x5982;&#x679C;&#x662F;&#x5E76;&#x884C;&#x6267;&#x884C;&#xFF08;&#x5373;&#x540C;&#x65F6;&#x7684;&#x5E73;&#x884C;&#x6267;&#x884C;&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>&amp;</code> &#x7B26;&#x53F7;&#x3002;</p>
<pre><code>$ npm run script1 &amp; npm run script2
</code></pre><p>&#x5982;&#x679C;&#x662F;&#x7EE7;&#x53D1;&#x6267;&#x884C;&#xFF08;&#x5373;&#x53EA;&#x6709;&#x524D;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x6210;&#x529F;&#xFF0C;&#x624D;&#x6267;&#x884C;&#x4E0B;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>&amp;&amp;</code> &#x7B26;&#x53F7;&#x3002;</p>
<pre><code>$ npm run script1 &amp;&amp; npm run script2
</code></pre><p><strong>&#x4E09;&#x3001;&#x7B80;&#x5199;&#x5F62;&#x5F0F;</strong></p>
<p>&#x5E38;&#x7528;&#x7684; npm &#x811A;&#x672C;&#x7B80;&#x5199;&#x5F62;&#x5F0F;&#x3002;</p>
<pre><code>npm start &#x662F; npm run start
</code></pre><p><strong>&#x56DB;&#x3001;&#x53D8;&#x91CF;</strong></p>
<p>npm &#x811A;&#x672C;&#x6709;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x5F3A;&#x5927;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; npm &#x7684;&#x5185;&#x90E8;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x901A;&#x8FC7; <code>npm_package_</code> &#x524D;&#x7F00;&#xFF0C;npm &#x811A;&#x672C;&#x53EF;&#x4EE5;&#x62FF;&#x5230; package.json &#x91CC;&#x9762;&#x7684;&#x5B57;&#x6BB5;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A; package.json&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x4E00;&#x5B9A;&#x8981;&#x5728; npm &#x811A;&#x672C;&#x4E2D;&#x8FD0;&#x884C;&#xFF08;&#x5982;&#xFF1A;npm run view&#xFF09;&#x624D;&#x53EF;&#x4EE5;&#xFF0C;&#x76F4;&#x63A5;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x4E2D;&#x8FD0;&#x884C;JS&#xFF08;&#x5982;&#xFF1A;node view.js&#xFF09;&#x662F;&#x62FF;&#x4E0D;&#x5230;&#x503C;&#x7684;</p>
</blockquote>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;foo&quot;</span>, 
  <span class="hljs-string">&quot;version&quot;</span>: <span class="hljs-string">&quot;1.2.5&quot;</span>,
  <span class="hljs-string">&quot;scripts&quot;</span>: {
    <span class="hljs-string">&quot;view&quot;</span>: <span class="hljs-string">&quot;node view.js&quot;</span>
  }
}
</code></pre>
<p>&#x90A3;&#x4E48;&#xFF0C;&#x53D8;&#x91CF; npm_package_name &#x8FD4;&#x56DE; foo&#xFF0C;&#x53D8;&#x91CF; npm_package_version &#x8FD4;&#x56DE; 1.2.5&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// view.js</span>
<span class="hljs-built_in">console</span>.log(process.env.npm_package_name); <span class="hljs-comment">// foo</span>
<span class="hljs-built_in">console</span>.log(process.env.npm_package_version); <span class="hljs-comment">// 1.2.5</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x73AF;&#x5883;&#x53D8;&#x91CF; process.env &#x5BF9;&#x8C61;&#xFF0C;&#x62FF;&#x5230; package.json &#x7684;&#x5B57;&#x6BB5;&#x503C;&#x3002;&#x5982;&#x679C;&#x662F; Bash &#x811A;&#x672C;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;$npm_package_name &#x548C; $npm_package_version &#x53D6;&#x5230;&#x8FD9;&#x4E24;&#x4E2A;&#x503C;&#x3002;</p>
<p>npm<em>package</em>&#x524D;&#x7F00;&#x4E5F;&#x652F;&#x6301;&#x5D4C;&#x5957;&#x7684;package.json&#x5B57;&#x6BB5;&#x3002;</p>
<pre><code class="lang-json"><span class="hljs-string">&quot;repository&quot;</span>: {
  <span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;git&quot;</span>,
  <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;xxx&quot;</span>
},
scripts: {
  <span class="hljs-string">&quot;view&quot;</span>: <span class="hljs-string">&quot;echo $npm_package_repository_type&quot;</span>
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;repository &#x5B57;&#x6BB5;&#x7684; type &#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; npm_package_repository_type &#x53D6;&#x5230;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code>&quot;scripts&quot;: {
  &quot;install&quot;: &quot;foo.js&quot;
}
</code></pre><p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;npm_package_scripts_install &#x53D8;&#x91CF;&#x7684;&#x503C;&#x7B49;&#x4E8E; foo.js&#x3002;</p>
<p>&#x7136;&#x540E;&#xFF0C;npm &#x811A;&#x672C;&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; npm<em>config</em> &#x524D;&#x7F00;&#xFF0C;&#x62FF;&#x5230; npm &#x7684;&#x914D;&#x7F6E;&#x53D8;&#x91CF;&#xFF0C;&#x5373; npm config get xxx &#x547D;&#x4EE4;&#x8FD4;&#x56DE;&#x7684;&#x503C;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x5F53;&#x524D;&#x6A21;&#x5757;&#x7684;&#x53D1;&#x884C;&#x6807;&#x7B7E;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; npm_config_tag &#x53D6;&#x5230;&#x3002;</p>
<pre><code>&quot;view&quot;: &quot;echo $npm_config_tag&quot;,
</code></pre><p>&#x6CE8;&#x610F;&#xFF0C;package.json &#x91CC;&#x9762;&#x7684; config &#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x88AB;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x8986;&#x76D6;&#x3002;</p>
<pre><code class="lang-json">{ 
  <span class="hljs-string">&quot;name&quot;</span> : <span class="hljs-string">&quot;foo&quot;</span>,
  <span class="hljs-string">&quot;config&quot;</span> : { <span class="hljs-string">&quot;port&quot;</span> : <span class="hljs-string">&quot;8080&quot;</span> },
  <span class="hljs-string">&quot;scripts&quot;</span> : { <span class="hljs-string">&quot;start&quot;</span> : <span class="hljs-string">&quot;node server.js&quot;</span> }
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;npm_package_config_port &#x53D8;&#x91CF;&#x8FD4;&#x56DE;&#x7684;&#x662F; 8080&#x3002;&#x8FD9;&#x4E2A;&#x503C;&#x53EF;&#x4EE5;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x8986;&#x76D6;&#x3002;</p>
<pre><code>$ npm config set foo:port 80
</code></pre><p>&#x6700;&#x540E;&#xFF0C;env&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x5217;&#x51FA;&#x6240;&#x6709;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3002;</p>
<p>&quot;env&quot;: &quot;env&quot;</p>
<h4 id="210-npm-&#x5B89;&#x88C5;-git-&#x4E0A;&#x53D1;&#x5E03;&#x7684;&#x5305;"><a name="210-npm-&#x5B89;&#x88C5;-git-&#x4E0A;&#x53D1;&#x5E03;&#x7684;&#x5305;" class="anchor-navigation-ex-anchor" href="#210-npm-&#x5B89;&#x88C5;-git-&#x4E0A;&#x53D1;&#x5E03;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="210-npm-&#x5B89;&#x88C5;-git-&#x4E0A;&#x53D1;&#x5E03;&#x7684;&#x5305;" class="plugin-anchor" href="#210-npm-&#x5B89;&#x88C5;-git-&#x4E0A;&#x53D1;&#x5E03;&#x7684;&#x5305;"><i class="fa fa-link" aria-hidden="true"></i></a>2.10 npm &#x5B89;&#x88C5; git &#x4E0A;&#x53D1;&#x5E03;&#x7684;&#x5305;</h4>
<pre><code># &#x8FD9;&#x6837;&#x9002;&#x5408;&#x5B89;&#x88C5;&#x516C;&#x53F8;&#x5185;&#x90E8;&#x7684;git&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x7684;&#x9879;&#x76EE;
npm install git+https://git@github.com:lurongtao/gp-project.git

# &#x6216;&#x8005;&#x4EE5;ssh&#x7684;&#x65B9;&#x5F0F;
npm install git+ssh://git@github.com:lurongtao/gp-project.git
</code></pre><h4 id="211-cross-env-&#x4F7F;&#x7528;"><a name="211-cross-env-&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#211-cross-env-&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="211-cross-env-&#x4F7F;&#x7528;" class="plugin-anchor" href="#211-cross-env-&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>2.11 cross-env &#x4F7F;&#x7528;</h4>
<h5 id="2111-cross-env&#x662F;&#x4EC0;&#x4E48;"><a name="2111-cross-env&#x662F;&#x4EC0;&#x4E48;" class="anchor-navigation-ex-anchor" href="#2111-cross-env&#x662F;&#x4EC0;&#x4E48;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2111-cross-env&#x662F;&#x4EC0;&#x4E48;" class="plugin-anchor" href="#2111-cross-env&#x662F;&#x4EC0;&#x4E48;"><i class="fa fa-link" aria-hidden="true"></i></a>2.11.1 cross-env&#x662F;&#x4EC0;&#x4E48;</h5>
<p>&#x8FD0;&#x884C;&#x8DE8;&#x5E73;&#x53F0;&#x8BBE;&#x7F6E;&#x548C;&#x4F7F;&#x7528;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x811A;&#x672C;</p>
<h5 id="2112-&#x51FA;&#x73B0;&#x539F;&#x56E0;"><a name="2112-&#x51FA;&#x73B0;&#x539F;&#x56E0;" class="anchor-navigation-ex-anchor" href="#2112-&#x51FA;&#x73B0;&#x539F;&#x56E0;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2112-&#x51FA;&#x73B0;&#x539F;&#x56E0;" class="plugin-anchor" href="#2112-&#x51FA;&#x73B0;&#x539F;&#x56E0;"><i class="fa fa-link" aria-hidden="true"></i></a>2.11.2 &#x51FA;&#x73B0;&#x539F;&#x56E0;</h5>
<p>&#x5F53;&#x60A8;&#x4F7F;&#x7528; NODE_ENV=production, &#x6765;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x5927;&#x591A;&#x6570; Windows &#x547D;&#x4EE4;&#x63D0;&#x793A;&#x5C06;&#x4F1A;&#x963B;&#x585E;(&#x62A5;&#x9519;)&#x3002;&#xFF08;&#x5F02;&#x5E38;&#x662F;Windows&#x4E0A;&#x7684;Bash&#xFF0C;&#x5B83;&#x4F7F;&#x7528;&#x672C;&#x673A;Bash&#x3002;&#xFF09;&#x6362;&#x8A00;&#x4E4B;&#xFF0C;Windows &#x4E0D;&#x652F;&#x6301; NODE_ENV=production &#x7684;&#x8BBE;&#x7F6E;&#x65B9;&#x5F0F;&#x3002;</p>
<h5 id="2113-&#x89E3;&#x51B3;"><a name="2113-&#x89E3;&#x51B3;" class="anchor-navigation-ex-anchor" href="#2113-&#x89E3;&#x51B3;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2113-&#x89E3;&#x51B3;" class="plugin-anchor" href="#2113-&#x89E3;&#x51B3;"><i class="fa fa-link" aria-hidden="true"></i></a>2.11.3 &#x89E3;&#x51B3;</h5>
<p>cross-env &#x4F7F;&#x5F97;&#x60A8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5355;&#x4E2A;&#x547D;&#x4EE4;&#xFF0C;&#x800C;&#x4E0D;&#x5FC5;&#x62C5;&#x5FC3;&#x4E3A;&#x5E73;&#x53F0;&#x6B63;&#x786E;&#x8BBE;&#x7F6E;&#x6216;&#x4F7F;&#x7528;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3002;&#x8FD9;&#x4E2A;&#x8FF7;&#x4F60;&#x7684;&#x5305;(cross-env)&#x80FD;&#x591F;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684; scripts&#xFF0C;&#x8BA9;&#x4F60;&#x80FD;&#x591F;&#x4EE5; Unix &#x65B9;&#x5F0F;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x7136;&#x540E;&#x5728; Windows &#x4E0A;&#x4E5F;&#x80FD;&#x517C;&#x5BB9;&#x8FD0;&#x884C;&#x3002;</p>
<h5 id="2114-&#x5B89;&#x88C5;"><a name="2114-&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#2114-&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2114-&#x5B89;&#x88C5;" class="plugin-anchor" href="#2114-&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>2.11.4 &#x5B89;&#x88C5;</h5>
<p>npm install --save-dev cross-env</p>
<h5 id="2115-&#x4F7F;&#x7528;"><a name="2115-&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#2115-&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2115-&#x4F7F;&#x7528;" class="plugin-anchor" href="#2115-&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>2.11.5 &#x4F7F;&#x7528;</h5>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;scripts&quot;</span>: {
    <span class="hljs-string">&quot;build&quot;</span>: <span class="hljs-string">&quot;cross-env NODE_ENV=production webpack --config build/webpack.config.js&quot;</span>
  }
}
</code></pre>
<p>NODE_ENV&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x5C06;&#x7531; cross-env &#x8BBE;&#x7F6E;
&#x6253;&#x5370; process.env.NODE_ENV === &apos;production&apos;</p>
<h3 id="3&#x3001;nrm-npm-registry-manager"><a name="3&#x3001;nrm-npm-registry-manager" class="anchor-navigation-ex-anchor" href="#3&#x3001;nrm-npm-registry-manager"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;nrm-npm-registry-manager" class="plugin-anchor" href="#3&#x3001;nrm-npm-registry-manager"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;NRM: npm registry manager</h3>
<h4 id="31-&#x624B;&#x5DE5;&#x5207;&#x6362;&#x6E90;"><a name="31-&#x624B;&#x5DE5;&#x5207;&#x6362;&#x6E90;" class="anchor-navigation-ex-anchor" href="#31-&#x624B;&#x5DE5;&#x5207;&#x6362;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="31-&#x624B;&#x5DE5;&#x5207;&#x6362;&#x6E90;" class="plugin-anchor" href="#31-&#x624B;&#x5DE5;&#x5207;&#x6362;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1 &#x624B;&#x5DE5;&#x5207;&#x6362;&#x6E90;</h4>
<h5 id="311-&#x67E5;&#x770B;&#x5F53;&#x524D;&#x6E90;"><a name="311-&#x67E5;&#x770B;&#x5F53;&#x524D;&#x6E90;" class="anchor-navigation-ex-anchor" href="#311-&#x67E5;&#x770B;&#x5F53;&#x524D;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="311-&#x67E5;&#x770B;&#x5F53;&#x524D;&#x6E90;" class="plugin-anchor" href="#311-&#x67E5;&#x770B;&#x5F53;&#x524D;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.1 &#x67E5;&#x770B;&#x5F53;&#x524D;&#x6E90;</h5>
<pre><code>npm config get registry
</code></pre><h5 id="312-&#x5207;&#x6362;&#x6DD8;&#x5B9D;&#x6E90;"><a name="312-&#x5207;&#x6362;&#x6DD8;&#x5B9D;&#x6E90;" class="anchor-navigation-ex-anchor" href="#312-&#x5207;&#x6362;&#x6DD8;&#x5B9D;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="312-&#x5207;&#x6362;&#x6DD8;&#x5B9D;&#x6E90;" class="plugin-anchor" href="#312-&#x5207;&#x6362;&#x6DD8;&#x5B9D;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.2 &#x5207;&#x6362;&#x6DD8;&#x5B9D;&#x6E90;</h5>
<pre><code>npm config set registry https://registry.npm.taobao.org
</code></pre><h4 id="32-nrm-&#x7BA1;&#x7406;&#x6E90;"><a name="32-nrm-&#x7BA1;&#x7406;&#x6E90;" class="anchor-navigation-ex-anchor" href="#32-nrm-&#x7BA1;&#x7406;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="32-nrm-&#x7BA1;&#x7406;&#x6E90;" class="plugin-anchor" href="#32-nrm-&#x7BA1;&#x7406;&#x6E90;"><i class="fa fa-link" aria-hidden="true"></i></a>3.2 NRM &#x7BA1;&#x7406;&#x6E90;</h4>
<p>NRM (npm registry manager)&#x662F;npm&#x7684;&#x955C;&#x50CF;&#x6E90;&#x7BA1;&#x7406;&#x5DE5;&#x5177;&#xFF0C;&#x6709;&#x65F6;&#x5019;&#x56FD;&#x5916;&#x8D44;&#x6E90;&#x592A;&#x6162;&#xFF0C;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5C31;&#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x5730;&#x5728; npm &#x6E90;&#x95F4;&#x5207;&#x6362;&#x3002;</p>
<h5 id="321-&#x5B89;&#x88C5;-nrm"><a name="321-&#x5B89;&#x88C5;-nrm" class="anchor-navigation-ex-anchor" href="#321-&#x5B89;&#x88C5;-nrm"><i class="fa fa-link" aria-hidden="true"></i></a><a name="321-&#x5B89;&#x88C5;-nrm" class="plugin-anchor" href="#321-&#x5B89;&#x88C5;-nrm"><i class="fa fa-link" aria-hidden="true"></i></a>3.2.1 &#x5B89;&#x88C5; nrm</h5>
<p>&#x5728;&#x547D;&#x4EE4;&#x884C;&#x6267;&#x884C;&#x547D;&#x4EE4;&#xFF0C;npm install -g nrm&#xFF0C;&#x5168;&#x5C40;&#x5B89;&#x88C5;nrm&#x3002;</p>
<h5 id="322-&#x4F7F;&#x7528;-nrm"><a name="322-&#x4F7F;&#x7528;-nrm" class="anchor-navigation-ex-anchor" href="#322-&#x4F7F;&#x7528;-nrm"><i class="fa fa-link" aria-hidden="true"></i></a><a name="322-&#x4F7F;&#x7528;-nrm" class="plugin-anchor" href="#322-&#x4F7F;&#x7528;-nrm"><i class="fa fa-link" aria-hidden="true"></i></a>3.2.2 &#x4F7F;&#x7528; nrm</h5>
<p>&#x6267;&#x884C;&#x547D;&#x4EE4; nrm ls &#x67E5;&#x770B;&#x53EF;&#x9009;&#x7684;&#x6E90;&#x3002;
&#x5176;&#x4E2D;&#xFF0C;&#x5E26;*&#x7684;&#x662F;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;&#x6E90;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x8F93;&#x51FA;&#x8868;&#x660E;&#x5F53;&#x524D;&#x6E90;&#x662F;&#x5B98;&#x65B9;&#x6E90;&#x3002;</p>
<h5 id="323-&#x5207;&#x6362;-nrm"><a name="323-&#x5207;&#x6362;-nrm" class="anchor-navigation-ex-anchor" href="#323-&#x5207;&#x6362;-nrm"><i class="fa fa-link" aria-hidden="true"></i></a><a name="323-&#x5207;&#x6362;-nrm" class="plugin-anchor" href="#323-&#x5207;&#x6362;-nrm"><i class="fa fa-link" aria-hidden="true"></i></a>3.2.3 &#x5207;&#x6362; nrm</h5>
<p>&#x5982;&#x679C;&#x8981;&#x5207;&#x6362;&#x5230;taobao&#x6E90;&#xFF0C;&#x6267;&#x884C;&#x547D;&#x4EE4;nrm use taobao&#x3002;</p>
<h5 id="324-&#x6D4B;&#x8BD5;&#x901F;&#x5EA6;"><a name="324-&#x6D4B;&#x8BD5;&#x901F;&#x5EA6;" class="anchor-navigation-ex-anchor" href="#324-&#x6D4B;&#x8BD5;&#x901F;&#x5EA6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="324-&#x6D4B;&#x8BD5;&#x901F;&#x5EA6;" class="plugin-anchor" href="#324-&#x6D4B;&#x8BD5;&#x901F;&#x5EA6;"><i class="fa fa-link" aria-hidden="true"></i></a>3.2.4 &#x6D4B;&#x8BD5;&#x901F;&#x5EA6;</h5>
<p>&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; nrm test &#x6D4B;&#x8BD5;&#x76F8;&#x5E94;&#x6E90;&#x7684;&#x54CD;&#x5E94;&#x65F6;&#x95F4;&#x3002;</p>
<pre><code>nrm test
</code></pre><h3 id="4&#x3001;npx-npm-package-extention"><a name="4&#x3001;npx-npm-package-extention" class="anchor-navigation-ex-anchor" href="#4&#x3001;npx-npm-package-extention"><i class="fa fa-link" aria-hidden="true"></i></a><a name="4&#x3001;npx-npm-package-extention" class="plugin-anchor" href="#4&#x3001;npx-npm-package-extention"><i class="fa fa-link" aria-hidden="true"></i></a>4&#x3001;NPX: npm package extention</h3>
<p>npm &#x4ECE;5.2&#x7248;&#x5F00;&#x59CB;&#xFF0C;&#x589E;&#x52A0;&#x4E86; npx &#x547D;&#x4EE4;&#x3002;&#x5B83;&#x6709;&#x5F88;&#x591A;&#x7528;&#x5904;&#xFF0C;&#x672C;&#x6587;&#x4ECB;&#x7ECD;&#x8BE5;&#x547D;&#x4EE4;&#x7684;&#x4E3B;&#x8981;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x3002;</p>
<p>Node &#x81EA;&#x5E26; npm &#x6A21;&#x5757;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528; npx &#x547D;&#x4EE4;&#x3002;&#x4E07;&#x4E00;&#x4E0D;&#x80FD;&#x7528;&#xFF0C;&#x5C31;&#x8981;&#x624B;&#x52A8;&#x5B89;&#x88C5;&#x4E00;&#x4E0B;&#x3002;</p>
<pre><code>$ npm install -g npx
</code></pre><h4 id="41-&#x8C03;&#x7528;&#x9879;&#x76EE;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;"><a name="41-&#x8C03;&#x7528;&#x9879;&#x76EE;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#41-&#x8C03;&#x7528;&#x9879;&#x76EE;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="41-&#x8C03;&#x7528;&#x9879;&#x76EE;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;" class="plugin-anchor" href="#41-&#x8C03;&#x7528;&#x9879;&#x76EE;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1 &#x8C03;&#x7528;&#x9879;&#x76EE;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;</h4>
<p>npx &#x60F3;&#x8981;&#x89E3;&#x51B3;&#x7684;&#x4E3B;&#x8981;&#x95EE;&#x9898;&#xFF0C;&#x5C31;&#x662F;&#x8C03;&#x7528;&#x9879;&#x76EE;&#x5185;&#x90E8;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x9879;&#x76EE;&#x5185;&#x90E8;&#x5B89;&#x88C5;&#x4E86;Mocha&#x3002;</p>
<pre><code>$ npm install -D mocha
</code></pre><p>&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x8C03;&#x7528; Mocha &#xFF0C;&#x53EA;&#x80FD;&#x5728;&#x9879;&#x76EE;&#x811A;&#x672C;&#x548C; package.json &#x7684;scripts&#x5B57;&#x6BB5;&#x91CC;&#x9762;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x4E0B;&#x8C03;&#x7528;&#xFF0C;&#x5FC5;&#x987B;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x3002;</p>
<pre><code># &#x9879;&#x76EE;&#x7684;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#x6267;&#x884C;
$ node-modules/.bin/mocha --version
</code></pre><p>npx &#x5C31;&#x662F;&#x60F3;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x8BA9;&#x9879;&#x76EE;&#x5185;&#x90E8;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;&#x7528;&#x8D77;&#x6765;&#x66F4;&#x65B9;&#x4FBF;&#xFF0C;&#x53EA;&#x8981;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x8C03;&#x7528;&#x5C31;&#x884C;&#x4E86;&#x3002;</p>
<pre><code>$ npx mocha --version
</code></pre><p>npx &#x7684;&#x539F;&#x7406;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x5C31;&#x662F;&#x8FD0;&#x884C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x5230;node_modules/.bin&#x8DEF;&#x5F84;&#x548C;&#x73AF;&#x5883;&#x53D8;&#x91CF;$PATH&#x91CC;&#x9762;&#xFF0C;&#x68C0;&#x67E5;&#x547D;&#x4EE4;&#x662F;&#x5426;&#x5B58;&#x5728;&#x3002;</p>
<p>&#x7531;&#x4E8E; npx &#x4F1A;&#x68C0;&#x67E5;&#x73AF;&#x5883;&#x53D8;&#x91CF;$PATH&#xFF0C;&#x6240;&#x4EE5;&#x7CFB;&#x7EDF;&#x547D;&#x4EE4;&#x4E5F;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x3002;</p>
<pre><code># &#x7B49;&#x540C;&#x4E8E; ls
$ npx ls
</code></pre><p>&#x6CE8;&#x610F;&#xFF0C;Bash &#x5185;&#x7F6E;&#x7684;&#x547D;&#x4EE4;&#x4E0D;&#x5728;$PATH&#x91CC;&#x9762;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x80FD;&#x7528;&#x3002;&#x6BD4;&#x5982;&#xFF0C;cd&#x662F; Bash &#x547D;&#x4EE4;&#xFF0C;&#x56E0;&#x6B64;&#x5C31;&#x4E0D;&#x80FD;&#x7528;npx cd&#x3002;</p>
<h4 id="42-&#x907F;&#x514D;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x6A21;&#x5757;"><a name="42-&#x907F;&#x514D;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#42-&#x907F;&#x514D;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="42-&#x907F;&#x514D;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x6A21;&#x5757;" class="plugin-anchor" href="#42-&#x907F;&#x514D;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>4.2 &#x907F;&#x514D;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x6A21;&#x5757;</h4>
<p>&#x9664;&#x4E86;&#x8C03;&#x7528;&#x9879;&#x76EE;&#x5185;&#x90E8;&#x6A21;&#x5757;&#xFF0C;npx &#x8FD8;&#x80FD;&#x907F;&#x514D;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x7684;&#x6A21;&#x5757;&#x3002;&#x6BD4;&#x5982;&#xFF0C;create-react-app &#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#x662F;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#xFF0C;npx &#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#x5B83;&#xFF0C;&#x800C;&#x4E14;&#x4E0D;&#x8FDB;&#x884C;&#x5168;&#x5C40;&#x5B89;&#x88C5;&#x3002;</p>
<pre><code>$ npx create-react-app my-react-app
</code></pre><p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x65F6;&#xFF0C;npx &#x5C06; create-react-app &#x4E0B;&#x8F7D;&#x5230;&#x4E00;&#x4E2A;&#x4E34;&#x65F6;&#x76EE;&#x5F55;&#xFF0C;&#x4F7F;&#x7528;&#x4EE5;&#x540E;&#x518D;&#x5220;&#x9664;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x4EE5;&#x540E;&#x518D;&#x6B21;&#x6267;&#x884C;&#x4E0A;&#x9762;&#x7684;&#x547D;&#x4EE4;&#xFF0C;&#x4F1A;&#x91CD;&#x65B0;&#x4E0B;&#x8F7D; create-react-app&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x53EA;&#x8981; npx &#x540E;&#x9762;&#x7684;&#x6A21;&#x5757;&#x65E0;&#x6CD5;&#x5728;&#x672C;&#x5730;&#x53D1;&#x73B0;&#xFF0C;&#x5C31;&#x4F1A;&#x4E0B;&#x8F7D;&#x540C;&#x540D;&#x6A21;&#x5757;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x672C;&#x5730;&#x6CA1;&#x6709;&#x5B89;&#x88C5;http-server&#x6A21;&#x5757;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x4F1A;&#x81EA;&#x52A8;&#x4E0B;&#x8F7D;&#x8BE5;&#x6A21;&#x5757;&#xFF0C;&#x5728;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x542F;&#x52A8;&#x4E00;&#x4E2A; Web &#x670D;&#x52A1;&#x3002;</p>
<pre><code>$ npx http-server
</code></pre><h4 id="43---no-install-&#x53C2;&#x6570;&#x548C;---ignore-existing-&#x53C2;&#x6570;"><a name="43---no-install-&#x53C2;&#x6570;&#x548C;---ignore-existing-&#x53C2;&#x6570;" class="anchor-navigation-ex-anchor" href="#43---no-install-&#x53C2;&#x6570;&#x548C;---ignore-existing-&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="43---no-install-&#x53C2;&#x6570;&#x548C;---ignore-existing-&#x53C2;&#x6570;" class="plugin-anchor" href="#43---no-install-&#x53C2;&#x6570;&#x548C;---ignore-existing-&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>4.3 --no-install &#x53C2;&#x6570;&#x548C; --ignore-existing &#x53C2;&#x6570;</h4>
<p>&#x5982;&#x679C;&#x60F3;&#x8BA9; npx &#x5F3A;&#x5236;&#x4F7F;&#x7528;&#x672C;&#x5730;&#x6A21;&#x5757;&#xFF0C;&#x4E0D;&#x4E0B;&#x8F7D;&#x8FDC;&#x7A0B;&#x6A21;&#x5757;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;--no-install&#x53C2;&#x6570;&#x3002;&#x5982;&#x679C;&#x672C;&#x5730;&#x4E0D;&#x5B58;&#x5728;&#x8BE5;&#x6A21;&#x5757;&#xFF0C;&#x5C31;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<pre><code>$ npx --no-install http-server
</code></pre><p>&#x53CD;&#x8FC7;&#x6765;&#xFF0C;&#x5982;&#x679C;&#x5FFD;&#x7565;&#x672C;&#x5730;&#x7684;&#x540C;&#x540D;&#x6A21;&#x5757;&#xFF0C;&#x5F3A;&#x5236;&#x5B89;&#x88C5;&#x4F7F;&#x7528;&#x8FDC;&#x7A0B;&#x6A21;&#x5757;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;--ignore-existing&#x53C2;&#x6570;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x672C;&#x5730;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x4E86;http-server&#xFF0C;&#x4F46;&#x8FD8;&#x662F;&#x60F3;&#x4F7F;&#x7528;&#x8FDC;&#x7A0B;&#x6A21;&#x5757;&#xFF0C;&#x5C31;&#x7528;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x3002;</p>
<pre><code>$ npx --ignore-existing http-server
</code></pre><h1 id="&#x4E09;&#x3001;&#x6A21;&#x5757;&#x5305;-&#x4E0E;-commonjs"><a name="&#x4E09;&#x3001;&#x6A21;&#x5757;&#x5305;-&#x4E0E;-commonjs" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;&#x6A21;&#x5757;&#x5305;-&#x4E0E;-commonjs"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E09;&#x3001;&#x6A21;&#x5757;&#x5305;-&#x4E0E;-commonjs" class="plugin-anchor" href="#&#x4E09;&#x3001;&#x6A21;&#x5757;&#x5305;-&#x4E0E;-commonjs"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x3001;&#x6A21;&#x5757;/&#x5305; &#x4E0E; CommonJS</h1>
<h3 id="1&#x3001;&#x6A21;&#x5757;&#x5305;&#x5206;&#x7C7B;"><a name="1&#x3001;&#x6A21;&#x5757;&#x5305;&#x5206;&#x7C7B;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x6A21;&#x5757;&#x5305;&#x5206;&#x7C7B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x6A21;&#x5757;&#x5305;&#x5206;&#x7C7B;" class="plugin-anchor" href="#1&#x3001;&#x6A21;&#x5757;&#x5305;&#x5206;&#x7C7B;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x6A21;&#x5757;/&#x5305;&#x5206;&#x7C7B;</h3>
<p>Node.js &#x6709;&#x4E09;&#x7C7B;&#x6A21;&#x5757;&#xFF0C;&#x5373;&#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;&#x3001;&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x6A21;&#x5757;&#x3001;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x6A21;&#x5757;&#x3002;</p>
<h4 id="11-&#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;"><a name="11-&#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#11-&#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="11-&#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;" class="plugin-anchor" href="#11-&#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1 &#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;</h4>
<p>Node.js &#x5185;&#x7F6E;&#x6A21;&#x5757;&#x53C8;&#x53EB;&#x6838;&#x5FC3;&#x6A21;&#x5757;&#xFF0C;Node.js&#x5B89;&#x88C5;&#x5B8C;&#x6210;&#x53EF;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x3002;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>)
<span class="hljs-keyword">var</span> extname = path.extname(<span class="hljs-string">&apos;index.html&apos;</span>)
<span class="hljs-built_in">console</span>.log(extname)
</code></pre>
<h4 id="12-&#x7B2C;&#x4E09;&#x65B9;&#x7684;nodejs&#x6A21;&#x5757;"><a name="12-&#x7B2C;&#x4E09;&#x65B9;&#x7684;nodejs&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#12-&#x7B2C;&#x4E09;&#x65B9;&#x7684;nodejs&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="12-&#x7B2C;&#x4E09;&#x65B9;&#x7684;nodejs&#x6A21;&#x5757;" class="plugin-anchor" href="#12-&#x7B2C;&#x4E09;&#x65B9;&#x7684;nodejs&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2 &#x7B2C;&#x4E09;&#x65B9;&#x7684;Node.js&#x6A21;&#x5757;</h4>
<p>&#x7B2C;&#x4E09;&#x65B9;&#x7684;Node.js&#x6A21;&#x5757;&#x6307;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x5B9E;&#x73B0;&#x67D0;&#x4E9B;&#x529F;&#x80FD;&#xFF0C;&#x53D1;&#x5E03;&#x7684;npmjs.org&#x4E0A;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x6309;&#x7167;&#x4E00;&#x5B9A;&#x7684;&#x5F00;&#x6E90;&#x534F;&#x8BAE;&#x4F9B;&#x793E;&#x7FA4;&#x4F7F;&#x7528;&#x3002;&#x5982;&#xFF1A;</p>
<pre><code>npm install chalk
</code></pre><pre><code class="lang-js"><span class="hljs-keyword">const</span> chalk = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;chalk&apos;</span>)
<span class="hljs-built_in">console</span>.log(chalk.blue(<span class="hljs-string">&apos;Hello world!&apos;</span>))
</code></pre>
<h4 id="13-&#x81EA;&#x5B9A;&#x4E49;&#x7684;nodejs&#x6A21;&#x5757;"><a name="13-&#x81EA;&#x5B9A;&#x4E49;&#x7684;nodejs&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#13-&#x81EA;&#x5B9A;&#x4E49;&#x7684;nodejs&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="13-&#x81EA;&#x5B9A;&#x4E49;&#x7684;nodejs&#x6A21;&#x5757;" class="plugin-anchor" href="#13-&#x81EA;&#x5B9A;&#x4E49;&#x7684;nodejs&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3 &#x81EA;&#x5B9A;&#x4E49;&#x7684;Node.js&#x6A21;&#x5757;</h4>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x7684;Node.js&#x6A21;&#x5757;&#xFF0C;&#x4E5F;&#x53EB;&#x6587;&#x4EF6;&#x6A21;&#x5757;&#xFF0C;&#x662F;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x5199;&#x7684;&#x4F9B;&#x81EA;&#x5DF1;&#x4F7F;&#x7528;&#x7684;&#x6A21;&#x5757;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x8FD9;&#x7C7B;&#x6A21;&#x5757;&#x53D1;&#x5E03;&#x5230;npmjs.org&#x4E0A;&#x5C31;&#x6210;&#x4E86;&#x5F00;&#x6E90;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x6A21;&#x5757;&#x3002;</p>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x6A21;&#x5757;&#x662F;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x52A8;&#x6001;&#x52A0;&#x8F7D;&#xFF0C;&#x9700;&#x8981;&#x5B8C;&#x6574;&#x7684;&#x8DEF;&#x5F84;&#x5206;&#x6790;&#x3001;&#x6587;&#x4EF6;&#x5B9A;&#x4F4D;&#x3001;&#x7F16;&#x8BD1;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x3001;&#x901F;&#x5EA6;&#x76F8;&#x6BD4;&#x6838;&#x5FC3;&#x6A21;&#x5757;&#x7A0D;&#x5FAE;&#x6162;&#x4E00;&#x4E9B;&#xFF0C;&#x4F46;&#x662F;&#x7528;&#x7684;&#x975E;&#x5E38;&#x591A;&#x3002;</p>
<h5 id="131-&#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x3001;&#x63A5;&#x53E3;&#x66B4;&#x9732;&#x548C;&#x5F15;&#x7528;&#x63A5;&#x53E3;"><a name="131-&#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x3001;&#x63A5;&#x53E3;&#x66B4;&#x9732;&#x548C;&#x5F15;&#x7528;&#x63A5;&#x53E3;" class="anchor-navigation-ex-anchor" href="#131-&#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x3001;&#x63A5;&#x53E3;&#x66B4;&#x9732;&#x548C;&#x5F15;&#x7528;&#x63A5;&#x53E3;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="131-&#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x3001;&#x63A5;&#x53E3;&#x66B4;&#x9732;&#x548C;&#x5F15;&#x7528;&#x63A5;&#x53E3;" class="plugin-anchor" href="#131-&#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x3001;&#x63A5;&#x53E3;&#x66B4;&#x9732;&#x548C;&#x5F15;&#x7528;&#x63A5;&#x53E3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.1 &#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x3001;&#x63A5;&#x53E3;&#x66B4;&#x9732;&#x548C;&#x5F15;&#x7528;&#x63A5;&#x53E3;</h5>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A;&#x516C;&#x5171;&#x7684;&#x529F;&#x80FD; &#x62BD;&#x79BB;&#x6210;&#x4E3A;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x7684; js  &#x6587;&#x4EF6; &#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#x91CC;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x5C5E;&#x6027;&#xFF0C;&#x5916;&#x9762;&#x662F;&#x6CA1;&#x6CD5;&#x8BBF;&#x95EE;&#x7684;&#x3002;&#x5982;&#x679C;&#x8981;&#x8BA9;&#x5916;&#x90E8;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x6A21;&#x5757;&#x91CC;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x5C5E;&#x6027;&#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x5728;&#x6A21;&#x5757;&#x91CC;&#x9762;&#x901A;&#x8FC7; exports &#x6216;&#x8005; module.exports &#x66B4;&#x9732;&#x5C5E;&#x6027;&#x6216;&#x8005;&#x65B9;&#x6CD5;&#x3002;</p>
<p>m1.js&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> name = <span class="hljs-string">&apos;gp19&apos;</span>

<span class="hljs-keyword">const</span> sayName = () =&gt; {
  <span class="hljs-built_in">console</span>.log(name)
}

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;module 1&apos;</span>)

<span class="hljs-comment">// &#x63A5;&#x53E3;&#x66B4;&#x9732;&#x65B9;&#x6CD5;&#x4E00;&#xFF1A;</span>
<span class="hljs-built_in">module</span>.exports = {
  say: sayName
}

<span class="hljs-comment">// &#x63A5;&#x53E3;&#x66B4;&#x9732;&#x65B9;&#x6CD5;&#x4E8C;&#xFF1A;</span>
exports.say = sayName

<span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF01;</span>
exports = {
  say: sayName
}
</code></pre>
<p>main.js&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> m1 = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./m1&apos;</span>)
m1.say()
</code></pre>
<h5 id="132-&#x6A21;&#x5757;&#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;"><a name="132-&#x6A21;&#x5757;&#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;" class="anchor-navigation-ex-anchor" href="#132-&#x6A21;&#x5757;&#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="132-&#x6A21;&#x5757;&#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;" class="plugin-anchor" href="#132-&#x6A21;&#x5757;&#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.2 &#x6A21;&#x5757;&#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;</h5>
<p>&#x7531;&#x4E8E; exports &#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x65B9;&#x5F0F;&#x4E0D;&#x5BF9;&#xFF0C;&#x4F1A;&#x5728;&#x4E24;&#x4E2A;&#x4E0D;&#x540C; js &#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5BFC;&#x81F4;&#x5176;&#x4E2D;&#x4E00;&#x4E2A; js &#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x53E6;&#x5916;&#x4E00;&#x4E2A; js &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4ECE;&#x800C;&#x5BFC;&#x81F4;&#x6267;&#x884C;&#x62A5;&#x9519;&#x3002;&#x5982;&#xFF1A;</p>
<ul>
<li>a.js</li>
</ul>
<pre><code class="lang-js">exports.done = <span class="hljs-literal">false</span>
<span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./b.js&apos;</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;in a, b.done = %j&apos;</span>, b.done)
exports.done = <span class="hljs-literal">true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;a done&apos;</span>)
</code></pre>
<ul>
<li>b.js</li>
</ul>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;b starting&apos;</span>)
exports.done = <span class="hljs-literal">false</span>
<span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./a.js&apos;</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;in b, a.done = %j&apos;</span>, a.done)
exports.done = <span class="hljs-literal">true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;b done&apos;</span>)
</code></pre>
<ul>
<li>main.js</li>
</ul>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;main starting&apos;</span>)
<span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./a.js&apos;</span>)
<span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./b.js&apos;</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;in main, a.done = %j, b.done = %j&apos;</span>, a.done, b.done)
</code></pre>
<p>main.js &#x9996;&#x5148;&#x4F1A; load a.js, &#x6B64;&#x65F6;&#x6267;&#x884C;&#x5230;const b = require(&apos;./b.js&apos;);&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x7A0B;&#x5E8F;&#x4F1A;&#x8F6C;&#x53BB;loadb.js, &#x5728;b.js&#x4E2D;&#x6267;&#x884C;&#x5230;const a = require(&apos;./a.js&apos;); &#x4E3A;&#x4E86;&#x9632;&#x6B62;&#x65E0;&#x9650;&#x5FAA;&#x73AF;&#xFF0C;&#x5C06;a.jsexports&#x7684;&#x672A;&#x5B8C;&#x6210;&#x526F;&#x672C;&#x8FD4;&#x56DE;&#x5230;b.js&#x6A21;&#x5757;&#x3002;&#x7136;&#x540E;b.js&#x5B8C;&#x6210;&#x52A0;&#x8F7D;&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x5BFC;&#x51FA;&#x5BF9;&#x8C61;&#x63D0;&#x4F9B;&#x7ED9;a.js&#x6A21;&#x5757;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x77E5;&#x9053;nodeJs&#x7684;&#x5BF9;&#x6BCF;&#x4E2A;js&#x6587;&#x4EF6;&#x8FDB;&#x884C;&#x4E86;&#x4E00;&#x5C42;&#x5305;&#x88C5;&#x79F0;&#x4E3A;module&#xFF0C;module&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;exports&#xFF0C;&#x5F53;&#x8C03;&#x7528;require(&apos;a.js&apos;)&#x7684;&#x65F6;&#x5019;&#x5176;&#x5B9E;&#x8FD4;&#x56DE;&#x7684;&#x662F;module.exports&#x5BF9;&#x8C61;&#xFF0C;module.exports&#x521D;&#x59CB;&#x5316;&#x4E3A;&#x4E00;&#x4E2A;{}&#x7A7A;&#x7684;object&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6267;&#x884C;&#x5230;b.js&#x4E2D;const a = require(&apos;./a.js&apos;);&#x65F6;&#x4E0D;&#x4F1A;load&#x65B0;&#x7684;a module, &#x800C;&#x662F;&#x5C06;&#x5DF2;&#x7ECF;load&#x4F46;&#x662F;&#x8FD8;&#x672A;&#x5B8C;&#x6210;&#x7684;a module&#x7684;exports&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x7ED9;b module&#xFF0C;&#x6240;&#x4EE5;b.js&#x62FF;&#x5230;&#x7684;&#x662F;a module&#x7684;exports&#x5BF9;&#x8C61;&#xFF0C;&#x5373;&#xFF1A;{done:false}, &#x867D;&#x7136;&#x5728;a.js&#x4E2D;exports.done&#x88AB;&#x4FEE;&#x6539;&#x6210;&#x4E86;true&#xFF0C;&#x4F46;&#x662F;&#x7531;&#x4E8E;&#x6B64;&#x65F6;a.js&#x672A;load&#x5B8C;&#x6210;&#xFF0C;&#x6240;&#x4EE5;&#x5728;b.js&#x8F93;&#x51FA;&#x7684;a module&#x7684;&#x5C5E;&#x6027;done&#x4E3A;false&#xFF0C;&#x800C;&#x5728;main.js&#x4E2D;&#x8F93;&#x51FA;&#x7684;a module&#x7684;&#x5C5E;&#x6027;done&#x4E3A;true. Nodejs&#x901A;&#x8FC7;&#x4E0A;&#x9762;&#x8FD9;&#x79CD;&#x8FD4;&#x56DE;&#x672A;&#x5B8C;&#x6210;exports&#x5BF9;&#x8C61;&#x6765;&#x89E3;&#x51B3;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x7684;&#x95EE;&#x9898;&#x3002; </p>
<h1 id="&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;"><a name="&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;" class="plugin-anchor" href="#&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB;&#x3001;&#x5E38;&#x7528;&#x5185;&#x7F6E;&#x6A21;&#x5757;</h1>
<p>&#x8FD9;&#x91CC;&#x4ECB;&#x7ECD;&#x51E0;&#x4E2A;&#x5E38;&#x7528;&#x7684;&#x5185;&#x7F6E;&#x6A21;&#x5757;&#xFF1A;url, querystring, http, events, fs, stream, readline, crypto, zlib</p>
<h3 id="1&#x3001;url"><a name="1&#x3001;url" class="anchor-navigation-ex-anchor" href="#1&#x3001;url"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;url" class="plugin-anchor" href="#1&#x3001;url"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;url</h3>
<h4 id="11-parse"><a name="11-parse" class="anchor-navigation-ex-anchor" href="#11-parse"><i class="fa fa-link" aria-hidden="true"></i></a><a name="11-parse" class="plugin-anchor" href="#11-parse"><i class="fa fa-link" aria-hidden="true"></i></a>1.1 parse</h4>
<p>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>)
<span class="hljs-keyword">const</span> urlString = <span class="hljs-string">&apos;https://www.baidu.com:443/ad/index.html?id=8&amp;name=mouse#tag=110&apos;</span>
<span class="hljs-keyword">const</span> parsedStr = url.parse(urlString)
<span class="hljs-built_in">console</span>.log(parsedStr)
</code></pre>
<h4 id="12-format"><a name="12-format" class="anchor-navigation-ex-anchor" href="#12-format"><i class="fa fa-link" aria-hidden="true"></i></a><a name="12-format" class="plugin-anchor" href="#12-format"><i class="fa fa-link" aria-hidden="true"></i></a>1.2 format</h4>
<p>url.format(urlObject)</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>)
<span class="hljs-keyword">const</span> urlObject = {
  protocol: <span class="hljs-string">&apos;https:&apos;</span>,
  slashes: <span class="hljs-literal">true</span>,
  auth: <span class="hljs-literal">null</span>,
  host: <span class="hljs-string">&apos;www.baidu.com:443&apos;</span>,
  port: <span class="hljs-string">&apos;443&apos;</span>,
  hostname: <span class="hljs-string">&apos;www.baidu.com&apos;</span>,
  hash: <span class="hljs-string">&apos;#tag=110&apos;</span>,
  search: <span class="hljs-string">&apos;?id=8&amp;name=mouse&apos;</span>,
  query: { id: <span class="hljs-string">&apos;8&apos;</span>, name: <span class="hljs-string">&apos;mouse&apos;</span> },
  pathname: <span class="hljs-string">&apos;/ad/index.html&apos;</span>,
  path: <span class="hljs-string">&apos;/ad/index.html?id=8&amp;name=mouse&apos;</span>,
  href: <span class="hljs-string">&apos;https://www.baidu.com:443/ad/index.html?id=8&amp;name=mouse#tag=110&apos;</span>
}
<span class="hljs-keyword">const</span> parsedObj = url.format(urlObject)
<span class="hljs-built_in">console</span>.log(parsedObj)
</code></pre>
<h4 id="13-resolve"><a name="13-resolve" class="anchor-navigation-ex-anchor" href="#13-resolve"><i class="fa fa-link" aria-hidden="true"></i></a><a name="13-resolve" class="plugin-anchor" href="#13-resolve"><i class="fa fa-link" aria-hidden="true"></i></a>1.3 resolve</h4>
<p>url.resolve(from, to)</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>)
<span class="hljs-keyword">var</span> a = url.resolve(<span class="hljs-string">&apos;/one/two/three&apos;</span>, <span class="hljs-string">&apos;four&apos;</span>)
<span class="hljs-keyword">var</span> b = url.resolve(<span class="hljs-string">&apos;http://example.com/&apos;</span>, <span class="hljs-string">&apos;/one&apos;</span>)
<span class="hljs-keyword">var</span> c = url.resolve(<span class="hljs-string">&apos;http://example.com/one&apos;</span>, <span class="hljs-string">&apos;/two&apos;</span>)
<span class="hljs-built_in">console</span>.log(a + <span class="hljs-string">&quot;,&quot;</span> + b + <span class="hljs-string">&quot;,&quot;</span> + c)
</code></pre>
<h3 id="2&#x3001;querystring"><a name="2&#x3001;querystring" class="anchor-navigation-ex-anchor" href="#2&#x3001;querystring"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;querystring" class="plugin-anchor" href="#2&#x3001;querystring"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;querystring</h3>
<h4 id="21-parse"><a name="21-parse" class="anchor-navigation-ex-anchor" href="#21-parse"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-parse" class="plugin-anchor" href="#21-parse"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 parse</h4>
<p>querystring.parse(str[, sep[, eq[, options]]])</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>)
<span class="hljs-keyword">var</span> qs = <span class="hljs-string">&apos;x=3&amp;y=4&apos;</span>
<span class="hljs-keyword">var</span> parsed = querystring.parse(qs)
<span class="hljs-built_in">console</span>.log(parsed)
</code></pre>
<h4 id="22-stringify"><a name="22-stringify" class="anchor-navigation-ex-anchor" href="#22-stringify"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-stringify" class="plugin-anchor" href="#22-stringify"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 stringify</h4>
<p>querystring.stringify(obj[, sep[, eq[, options]]])</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>)
<span class="hljs-keyword">var</span> qo = {
  x: <span class="hljs-number">3</span>,
  y: <span class="hljs-number">4</span>
}
<span class="hljs-keyword">var</span> parsed = querystring.stringify(qo)
<span class="hljs-built_in">console</span>.log(parsed)
</code></pre>
<h4 id="23-escapeunescape"><a name="23-escapeunescape" class="anchor-navigation-ex-anchor" href="#23-escapeunescape"><i class="fa fa-link" aria-hidden="true"></i></a><a name="23-escapeunescape" class="plugin-anchor" href="#23-escapeunescape"><i class="fa fa-link" aria-hidden="true"></i></a>2.3 escape/unescape</h4>
<p>querystring.escape(str)</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>)
<span class="hljs-keyword">var</span> str = <span class="hljs-string">&apos;id=3&amp;city=&#x5317;&#x4EAC;&amp;url=https://www.baidu.com&apos;</span>
<span class="hljs-keyword">var</span> escaped = querystring.escape(str)
<span class="hljs-built_in">console</span>.log(escaped)
</code></pre>
<p>querystring.unescape(str)</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>)
<span class="hljs-keyword">var</span> str = <span class="hljs-string">&apos;id%3D3%26city%3D%E5%8C%97%E4%BA%AC%26url%3Dhttps%3A%2F%2Fwww.baidu.com&apos;</span>
<span class="hljs-keyword">var</span> unescaped = querystring.unescape(str)
<span class="hljs-built_in">console</span>.log(unescaped)
</code></pre>
<h3 id="3&#x3001;httphttps"><a name="3&#x3001;httphttps" class="anchor-navigation-ex-anchor" href="#3&#x3001;httphttps"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;httphttps" class="plugin-anchor" href="#3&#x3001;httphttps"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;http/https</h3>
<h4 id="31-get"><a name="31-get" class="anchor-navigation-ex-anchor" href="#31-get"><i class="fa fa-link" aria-hidden="true"></i></a><a name="31-get" class="plugin-anchor" href="#31-get"><i class="fa fa-link" aria-hidden="true"></i></a>3.1 get</h4>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>)
<span class="hljs-keyword">var</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;https&apos;</span>)

<span class="hljs-comment">// 1&#x3001;&#x63A5;&#x53E3; 2&#x3001;&#x8DE8;&#x57DF;</span>
<span class="hljs-keyword">const</span> server = http.createServer((request, response) =&gt; {
  <span class="hljs-keyword">var</span> url = request.url.substr(<span class="hljs-number">1</span>)

  <span class="hljs-keyword">var</span> data = <span class="hljs-string">&apos;&apos;</span>

  response.writeHeader(<span class="hljs-number">200</span>, {
    <span class="hljs-string">&apos;content-type&apos;</span>: <span class="hljs-string">&apos;application/json;charset=utf-8&apos;</span>,
    <span class="hljs-string">&apos;Access-Control-Allow-Origin&apos;</span>: <span class="hljs-string">&apos;*&apos;</span>
  })

  https.get(<span class="hljs-string">`https://m.lagou.com/listmore.json<span class="hljs-subst">${url}</span>`</span>, (res) =&gt; {

    res.on(<span class="hljs-string">&apos;data&apos;</span>, (chunk) =&gt; {
      data += chunk
    })

    res.on(<span class="hljs-string">&apos;end&apos;</span>, () =&gt; {
      response.end(<span class="hljs-built_in">JSON</span>.stringify({
        ret: <span class="hljs-literal">true</span>,
        data
      }))
    })
  })

})

server.listen(<span class="hljs-number">8080</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;localhost:8080&apos;</span>)
})
</code></pre>
<h4 id="32-post&#xFF1A;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#xFF08;&#x653B;&#x51FB;&#xFF09;"><a name="32-post&#xFF1A;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#xFF08;&#x653B;&#x51FB;&#xFF09;" class="anchor-navigation-ex-anchor" href="#32-post&#xFF1A;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#xFF08;&#x653B;&#x51FB;&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="32-post&#xFF1A;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#xFF08;&#x653B;&#x51FB;&#xFF09;" class="plugin-anchor" href="#32-post&#xFF1A;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#xFF08;&#x653B;&#x51FB;&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>3.2 post&#xFF1A;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#xFF08;&#x653B;&#x51FB;&#xFF09;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;https&apos;</span>)
<span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>)

<span class="hljs-keyword">const</span> postData = querystring.stringify({
  province: <span class="hljs-string">&apos;&#x4E0A;&#x6D77;&apos;</span>,
  city: <span class="hljs-string">&apos;&#x4E0A;&#x6D77;&apos;</span>,
  district: <span class="hljs-string">&apos;&#x5B9D;&#x5C71;&#x533A;&apos;</span>,
  address: <span class="hljs-string">&apos;&#x540C;&#x6D4E;&#x652F;&#x8DEF;199&#x53F7;&#x667A;&#x6167;&#x4E03;&#x7ACB;&#x65B9;3&#x53F7;&#x697C;2-4&#x5C42;&apos;</span>,
  latitude: <span class="hljs-number">43.0</span>,
  longitude: <span class="hljs-number">160.0</span>,
  message: <span class="hljs-string">&apos;&#x6C42;&#x8D2D;&#x4E00;&#x6761;&#x5C0F;&#x9C7C;&apos;</span>,
  contact: <span class="hljs-string">&apos;13666666&apos;</span>,
  type: <span class="hljs-string">&apos;sell&apos;</span>,
  time: <span class="hljs-number">1571217561</span>
})

<span class="hljs-keyword">const</span> options = {
  protocol: <span class="hljs-string">&apos;https:&apos;</span>,
  hostname: <span class="hljs-string">&apos;ik9hkddr.qcloud.la&apos;</span>,
  method: <span class="hljs-string">&apos;POST&apos;</span>,
  port: <span class="hljs-number">443</span>,
  path: <span class="hljs-string">&apos;/index.php/trade/add_item&apos;</span>,
  headers: {
    <span class="hljs-string">&apos;Content-Type&apos;</span>: <span class="hljs-string">&apos;application/x-www-form-urlencoded&apos;</span>,
    <span class="hljs-string">&apos;Content-Length&apos;</span>: Buffer.byteLength(postData)
  }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doPost</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> data

  <span class="hljs-keyword">let</span> req = https.request(options, (res) =&gt; {
    res.on(<span class="hljs-string">&apos;data&apos;</span>, chunk =&gt; data += chunk)
    res.on(<span class="hljs-string">&apos;end&apos;</span>, () =&gt; {
      <span class="hljs-built_in">console</span>.log(data)
    })
  })

  req.write(postData)
  req.end()
}

<span class="hljs-comment">// setInterval(() =&gt; {</span>
<span class="hljs-comment">//   doPost()</span>
<span class="hljs-comment">// }, 1000)</span>
</code></pre>
<h4 id="33-&#x8DE8;&#x57DF;&#xFF1A;jsonp"><a name="33-&#x8DE8;&#x57DF;&#xFF1A;jsonp" class="anchor-navigation-ex-anchor" href="#33-&#x8DE8;&#x57DF;&#xFF1A;jsonp"><i class="fa fa-link" aria-hidden="true"></i></a><a name="33-&#x8DE8;&#x57DF;&#xFF1A;jsonp" class="plugin-anchor" href="#33-&#x8DE8;&#x57DF;&#xFF1A;jsonp"><i class="fa fa-link" aria-hidden="true"></i></a>3.3 &#x8DE8;&#x57DF;&#xFF1A;jsonp</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>)
<span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>)

<span class="hljs-keyword">const</span> app = http.createServer((req, res) =&gt; {
  <span class="hljs-keyword">let</span> urlObj = url.parse(req.url, <span class="hljs-literal">true</span>)

  <span class="hljs-keyword">switch</span> (urlObj.pathname) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/api/user&apos;</span>:
      res.end(<span class="hljs-string">`<span class="hljs-subst">${urlObj.query.cb}</span>({&quot;name&quot;: &quot;gp145&quot;})`</span>)
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">default</span>:
      res.end(<span class="hljs-string">&apos;404.&apos;</span>)
      <span class="hljs-keyword">break</span>
  }
})

app.listen(<span class="hljs-number">8080</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;localhost:8080&apos;</span>)
})
</code></pre>
<h4 id="34-&#x8DE8;&#x57DF;&#xFF1A;cors"><a name="34-&#x8DE8;&#x57DF;&#xFF1A;cors" class="anchor-navigation-ex-anchor" href="#34-&#x8DE8;&#x57DF;&#xFF1A;cors"><i class="fa fa-link" aria-hidden="true"></i></a><a name="34-&#x8DE8;&#x57DF;&#xFF1A;cors" class="plugin-anchor" href="#34-&#x8DE8;&#x57DF;&#xFF1A;cors"><i class="fa fa-link" aria-hidden="true"></i></a>3.4 &#x8DE8;&#x57DF;&#xFF1A;CORS</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>)
<span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>)
<span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>)

<span class="hljs-keyword">const</span> app = http.createServer((req, res) =&gt; {
  <span class="hljs-keyword">let</span> data = <span class="hljs-string">&apos;&apos;</span>
  <span class="hljs-keyword">let</span> urlObj = url.parse(req.url, <span class="hljs-literal">true</span>)

  res.writeHead(<span class="hljs-number">200</span>, {
    <span class="hljs-string">&apos;content-type&apos;</span>: <span class="hljs-string">&apos;application/json;charset=utf-8&apos;</span>,
    <span class="hljs-string">&apos;Access-Control-Allow-Origin&apos;</span>: <span class="hljs-string">&apos;*&apos;</span>
  })

  req.on(<span class="hljs-string">&apos;data&apos;</span>, (chunk) =&gt; {
    data += chunk
  })

  req.on(<span class="hljs-string">&apos;end&apos;</span>, () =&gt; {
    responseResult(querystring.parse(data))
  })

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">responseResult</span>(<span class="hljs-params">data</span>) </span>{
    <span class="hljs-keyword">switch</span> (urlObj.pathname) {
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/api/login&apos;</span>:
        res.end(<span class="hljs-built_in">JSON</span>.stringify({
          message: data
        }))
        <span class="hljs-keyword">break</span>
      <span class="hljs-keyword">default</span>:
        res.end(<span class="hljs-string">&apos;404.&apos;</span>)
        <span class="hljs-keyword">break</span>
    }
  }
})

app.listen(<span class="hljs-number">8080</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;localhost:8080&apos;</span>)
})
</code></pre>
<h4 id="35-&#x8DE8;&#x57DF;&#xFF1A;middleware&#xFF08;http-proxy-middware&#xFF09;"><a name="35-&#x8DE8;&#x57DF;&#xFF1A;middleware&#xFF08;http-proxy-middware&#xFF09;" class="anchor-navigation-ex-anchor" href="#35-&#x8DE8;&#x57DF;&#xFF1A;middleware&#xFF08;http-proxy-middware&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="35-&#x8DE8;&#x57DF;&#xFF1A;middleware&#xFF08;http-proxy-middware&#xFF09;" class="plugin-anchor" href="#35-&#x8DE8;&#x57DF;&#xFF1A;middleware&#xFF08;http-proxy-middware&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>3.5 &#x8DE8;&#x57DF;&#xFF1A;middleware&#xFF08;http-proxy-middware&#xFF09;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>)
<span class="hljs-keyword">const</span> proxy = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http-proxy-middleware&apos;</span>)

http.createServer((req, res) =&gt; {
  <span class="hljs-keyword">let</span> url = req.url

  res.writeHead(<span class="hljs-number">200</span>, {
    <span class="hljs-string">&apos;Access-Control-Allow-Origin&apos;</span>: <span class="hljs-string">&apos;*&apos;</span>
  })

  <span class="hljs-keyword">if</span> (<span class="hljs-regexp">/^\/api/</span>.test(url)) {
    <span class="hljs-keyword">let</span> apiProxy = proxy(<span class="hljs-string">&apos;/api&apos;</span>, { 
      target: <span class="hljs-string">&apos;https://m.lagou.com&apos;</span>,
      changeOrigin: <span class="hljs-literal">true</span>,
      pathRewrite: {
        <span class="hljs-string">&apos;^/api&apos;</span>: <span class="hljs-string">&apos;&apos;</span>
      }
    })

    <span class="hljs-comment">// http-proy-middleware &#x5728;Node.js&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x65B9;&#x6CD5;</span>
    apiProxy(req, res)
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">switch</span> (url) {
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/index.html&apos;</span>:
        res.end(<span class="hljs-string">&apos;index.html&apos;</span>)
        <span class="hljs-keyword">break</span>
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/search.html&apos;</span>:
        res.end(<span class="hljs-string">&apos;search.html&apos;</span>)
        <span class="hljs-keyword">break</span>
      <span class="hljs-keyword">default</span>:
        res.end(<span class="hljs-string">&apos;[404]page not found.&apos;</span>)
    }
  }
}).listen(<span class="hljs-number">8080</span>)
</code></pre>
<h4 id="36-&#x722C;&#x866B;"><a name="36-&#x722C;&#x866B;" class="anchor-navigation-ex-anchor" href="#36-&#x722C;&#x866B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="36-&#x722C;&#x866B;" class="plugin-anchor" href="#36-&#x722C;&#x866B;"><i class="fa fa-link" aria-hidden="true"></i></a>3.6 &#x722C;&#x866B;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;https&apos;</span>)
<span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>)
<span class="hljs-keyword">const</span> cheerio = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cheerio&apos;</span>)

http.createServer((request, response) =&gt; {
  response.writeHead(<span class="hljs-number">200</span>, {
    <span class="hljs-string">&apos;content-type&apos;</span>: <span class="hljs-string">&apos;application/json;charset=utf-8&apos;</span>
  })

  <span class="hljs-keyword">const</span> options = {
    protocol: <span class="hljs-string">&apos;https:&apos;</span>,
    hostname: <span class="hljs-string">&apos;maoyan.com&apos;</span>,
    port: <span class="hljs-number">443</span>,
    path: <span class="hljs-string">&apos;/&apos;</span>,
    method: <span class="hljs-string">&apos;GET&apos;</span>
  }

  <span class="hljs-keyword">const</span> req = https.request(options, (res) =&gt; {
    <span class="hljs-keyword">let</span> data = <span class="hljs-string">&apos;&apos;</span>
    res.on(<span class="hljs-string">&apos;data&apos;</span>, (chunk) =&gt; {
      data += chunk
    })

    res.on(<span class="hljs-string">&apos;end&apos;</span>, () =&gt; {
      filterData(data)
    })
  })

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">filterData</span>(<span class="hljs-params">data</span>) </span>{
    <span class="hljs-keyword">let</span> $ = cheerio.load(data)
    <span class="hljs-keyword">let</span> $movieList = $(<span class="hljs-string">&apos;.movie-item&apos;</span>)
    <span class="hljs-keyword">let</span> movies = []
    $movieList.each((index, value) =&gt; {
      movies.push({
        title: $(value).find(<span class="hljs-string">&apos;.movie-title&apos;</span>).attr(<span class="hljs-string">&apos;title&apos;</span>),
        score: $(value).find(<span class="hljs-string">&apos;.movie-score i&apos;</span>).text(),
      })
    })

    response.end(<span class="hljs-built_in">JSON</span>.stringify(movies))
  }

  req.end()
}).listen(<span class="hljs-number">9000</span>)
</code></pre>
<h3 id="4&#x3001;events"><a name="4&#x3001;events" class="anchor-navigation-ex-anchor" href="#4&#x3001;events"><i class="fa fa-link" aria-hidden="true"></i></a><a name="4&#x3001;events" class="plugin-anchor" href="#4&#x3001;events"><i class="fa fa-link" aria-hidden="true"></i></a>4&#x3001;Events</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> EventEmitter = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;events&apos;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyEventEmitter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">EventEmitter</span> </span>{}

<span class="hljs-keyword">const</span> event = <span class="hljs-keyword">new</span> MyEventEmitter()

event.on(<span class="hljs-string">&apos;play&apos;</span>, (movie) =&gt; {
  <span class="hljs-built_in">console</span>.log(movie)
})

event.emit(<span class="hljs-string">&apos;play&apos;</span>, <span class="hljs-string">&apos;&#x6211;&#x548C;&#x6211;&#x7684;&#x7956;&#x56FD;&apos;</span>)
event.emit(<span class="hljs-string">&apos;play&apos;</span>, <span class="hljs-string">&apos;&#x4E2D;&#x56FD;&#x673A;&#x957F;&apos;</span>)
</code></pre>
<h3 id="5&#x3001;file-system"><a name="5&#x3001;file-system" class="anchor-navigation-ex-anchor" href="#5&#x3001;file-system"><i class="fa fa-link" aria-hidden="true"></i></a><a name="5&#x3001;file-system" class="plugin-anchor" href="#5&#x3001;file-system"><i class="fa fa-link" aria-hidden="true"></i></a>5&#x3001;File System</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)
<span class="hljs-keyword">const</span> fsP = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>).promises

<span class="hljs-comment">// &#x521B;&#x5EFA;&#x6587;&#x4EF6;&#x5939;</span>
fs.mkdir(<span class="hljs-string">&apos;./logs&apos;</span>, (err) =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;done.&apos;</span>)
})

<span class="hljs-comment">// &#x6587;&#x4EF6;&#x5939;&#x6539;&#x540D;</span>
fs.rename(<span class="hljs-string">&apos;./logs&apos;</span>, <span class="hljs-string">&apos;./log&apos;</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;done&apos;</span>)
})

<span class="hljs-comment">// &#x5220;&#x9664;&#x6587;&#x4EF6;&#x5939;</span>
fs.rmdir(<span class="hljs-string">&apos;./log&apos;</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;done.&apos;</span>)
})

<span class="hljs-comment">// &#x5199;&#x5185;&#x5BB9;&#x5230;&#x6587;&#x4EF6;&#x91CC;</span>
fs.writeFile(
  <span class="hljs-string">&apos;./logs/log1.txt&apos;</span>,
  <span class="hljs-string">&apos;hello&apos;</span>,
  <span class="hljs-comment">// &#x9519;&#x8BEF;&#x4F18;&#x5148;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</span>
  (err) =&gt; {
    <span class="hljs-keyword">if</span> (err) {
      <span class="hljs-built_in">console</span>.log(err.message)
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6587;&#x4EF6;&#x521B;&#x5EFA;&#x6210;&#x529F;&apos;</span>)
    }
  }
)

<span class="hljs-comment">// &#x7ED9;&#x6587;&#x4EF6;&#x8FFD;&#x52A0;&#x5185;&#x5BB9;</span>
fs.appendFile(<span class="hljs-string">&apos;./logs/log1.txt&apos;</span>, <span class="hljs-string">&apos;\nworld&apos;</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;done.&apos;</span>)
})

<span class="hljs-comment">// &#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x5185;&#x5BB9;</span>
fs.readFile(<span class="hljs-string">&apos;./logs/log1.txt&apos;</span>, <span class="hljs-string">&apos;utf-8&apos;</span>, (err, data) =&gt; {
  <span class="hljs-built_in">console</span>.log(data)
})

<span class="hljs-comment">// &#x5220;&#x9664;&#x6587;&#x4EF6;</span>
fs.unlink(<span class="hljs-string">&apos;./logs/log1.txt&apos;</span>, (err) =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;done.&apos;</span>)
})

<span class="hljs-comment">// &#x6279;&#x91CF;&#x5199;&#x6587;&#x4EF6;</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
  fs.writeFile(<span class="hljs-string">`./logs/log-<span class="hljs-subst">${i}</span>.txt`</span>, <span class="hljs-string">`log-<span class="hljs-subst">${i}</span>`</span>, (err) =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;done.&apos;</span>)
  })
}

<span class="hljs-comment">// &#x8BFB;&#x53D6;&#x6587;&#x4EF6;/&#x76EE;&#x5F55;&#x4FE1;&#x606F;</span>
fs.readdir(<span class="hljs-string">&apos;./&apos;</span>, (err, data) =&gt; {
  data.forEach((value, index) =&gt; {
    fs.stat(<span class="hljs-string">`./<span class="hljs-subst">${value}</span>`</span>, (err, stats) =&gt; {
      <span class="hljs-comment">// console.log(value + &apos;:&apos; + stats.size)</span>
      <span class="hljs-built_in">console</span>.log(value + <span class="hljs-string">&apos; is &apos;</span> + (stats.isDirectory() ? <span class="hljs-string">&apos;directory&apos;</span> : <span class="hljs-string">&apos;file&apos;</span>))
    })
  })
})

<span class="hljs-comment">// &#x540C;&#x6B65;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;</span>
<span class="hljs-keyword">try</span> {
  <span class="hljs-keyword">const</span> content = fs.readFileSync(<span class="hljs-string">&apos;./logs/log-1.txt&apos;</span>, <span class="hljs-string">&apos;utf-8&apos;</span>)
  <span class="hljs-built_in">console</span>.log(content)
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">0</span>)
} <span class="hljs-keyword">catch</span> (e) {
  <span class="hljs-built_in">console</span>.log(e.message)
}

<span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span>)

<span class="hljs-comment">// &#x5F02;&#x6B65;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#xFF1A;&#x65B9;&#x6CD5;&#x4E00;</span>
fs.readFile(<span class="hljs-string">&apos;./logs/log-0.txt&apos;</span>, <span class="hljs-string">&apos;utf-8&apos;</span>, (err, content) =&gt; {
  <span class="hljs-built_in">console</span>.log(content)
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">0</span>)
})
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span>)

<span class="hljs-comment">// &#x5F02;&#x6B65;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#xFF1A;&#x65B9;&#x6CD5;&#x4E8C;</span>
fs.readFile(<span class="hljs-string">&apos;./logs/log-0.txt&apos;</span>, <span class="hljs-string">&apos;utf-8&apos;</span>).then(result =&gt; {
  <span class="hljs-built_in">console</span>.log(result)
})

<span class="hljs-comment">// &#x5F02;&#x6B65;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#xFF1A;&#x65B9;&#x6CD5;&#x4E09;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getFile</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve) =&gt; {
    fs.readFile(<span class="hljs-string">&apos;./logs/log-0.txt&apos;</span>, <span class="hljs-string">&apos;utf-8&apos;</span>, (err, data) =&gt; {
      resolve(data)
    })
  })
}

;(<span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">await</span> getFile())
})()

<span class="hljs-comment">// &#x5F02;&#x6B65;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#xFF1A;&#x65B9;&#x6CD5;&#x56DB;</span>
<span class="hljs-keyword">const</span> fsp = fsP.readFile(<span class="hljs-string">&apos;./logs/log-1.txt&apos;</span>, <span class="hljs-string">&apos;utf-8&apos;</span>).then((result) =&gt; {
  <span class="hljs-built_in">console</span>.log(result)
})

<span class="hljs-built_in">console</span>.log(fsP)

<span class="hljs-comment">// watch &#x76D1;&#x6D4B;&#x6587;&#x4EF6;&#x53D8;&#x5316;</span>
fs.watch(<span class="hljs-string">&apos;./logs/log-0.txt&apos;</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">0</span>)
})
</code></pre>
<h3 id="6&#x3001;stream"><a name="6&#x3001;stream" class="anchor-navigation-ex-anchor" href="#6&#x3001;stream"><i class="fa fa-link" aria-hidden="true"></i></a><a name="6&#x3001;stream" class="plugin-anchor" href="#6&#x3001;stream"><i class="fa fa-link" aria-hidden="true"></i></a>6&#x3001;Stream</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)

<span class="hljs-keyword">const</span> readstream = fs.createReadStream(<span class="hljs-string">&apos;./note.txt&apos;</span>)
<span class="hljs-keyword">const</span> writestream = fs.createWriteStream(<span class="hljs-string">&apos;./note2.txt&apos;</span>)

writestream.write(readstream)
</code></pre>
<h3 id="7&#x3001;zlib"><a name="7&#x3001;zlib" class="anchor-navigation-ex-anchor" href="#7&#x3001;zlib"><i class="fa fa-link" aria-hidden="true"></i></a><a name="7&#x3001;zlib" class="plugin-anchor" href="#7&#x3001;zlib"><i class="fa fa-link" aria-hidden="true"></i></a>7&#x3001;Zlib</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)
<span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;zlib&apos;</span>)

<span class="hljs-keyword">const</span> gzip = zlib.createGzip()

<span class="hljs-keyword">const</span> readstream = fs.createReadStream(<span class="hljs-string">&apos;./note.txt&apos;</span>)
<span class="hljs-keyword">const</span> writestream = fs.createWriteStream(<span class="hljs-string">&apos;./note2.txt&apos;</span>)

readstream
  .pipe(gzip)
  .pipe(writestream)

writestream.write(readstream)
</code></pre>
<h3 id="8&#x3001;readline"><a name="8&#x3001;readline" class="anchor-navigation-ex-anchor" href="#8&#x3001;readline"><i class="fa fa-link" aria-hidden="true"></i></a><a name="8&#x3001;readline" class="plugin-anchor" href="#8&#x3001;readline"><i class="fa fa-link" aria-hidden="true"></i></a>8&#x3001;ReadLine</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;readline&apos;</span>)

<span class="hljs-keyword">const</span> rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

rl.question(<span class="hljs-string">&apos;What do you think of Node.js? &apos;</span>, (answer) =&gt; {
  <span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database</span>
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Thank you for your valuable feedback: <span class="hljs-subst">${answer}</span>`</span>)

  rl.close()
})
</code></pre>
<h3 id="9&#x3001;crypto"><a name="9&#x3001;crypto" class="anchor-navigation-ex-anchor" href="#9&#x3001;crypto"><i class="fa fa-link" aria-hidden="true"></i></a><a name="9&#x3001;crypto" class="plugin-anchor" href="#9&#x3001;crypto"><i class="fa fa-link" aria-hidden="true"></i></a>9&#x3001;Crypto</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>)

<span class="hljs-keyword">const</span> secret = <span class="hljs-string">&apos;abcdefg&apos;</span>
<span class="hljs-keyword">const</span> hash = crypto.createHmac(<span class="hljs-string">&apos;sha256&apos;</span>, secret)
                   .update(<span class="hljs-string">&apos;I love you&apos;</span>)
                   .digest(<span class="hljs-string">&apos;hex&apos;</span>)
<span class="hljs-built_in">console</span>.log(hash)
</code></pre>
<h1 id="&#x56DB;&#x3001;&#x8DEF;&#x7531;"><a name="&#x56DB;&#x3001;&#x8DEF;&#x7531;" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x56DB;&#x3001;&#x8DEF;&#x7531;" class="plugin-anchor" href="#&#x56DB;&#x3001;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB;&#x3001;&#x8DEF;&#x7531;</h1>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>)
<span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)

http.createServer( <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"> req, res </span>) </span>{

  <span class="hljs-keyword">switch</span> ( req.url ) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/home&apos;</span>:
      res.write(<span class="hljs-string">&apos;home&apos;</span>)
      res.end()
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/mine&apos;</span>:
      res.write(<span class="hljs-string">&apos;mine&apos;</span>)
      res.end()
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/login&apos;</span>: 
      fs.readFile( <span class="hljs-string">&apos;./static/login.html&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"> error , data </span>) </span>{
        <span class="hljs-keyword">if</span> ( error ) <span class="hljs-keyword">throw</span> error  
        res.write( data )
        res.end()
      })
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/fulian.jpg&apos;</span>:
      fs.readFile( <span class="hljs-string">&apos;./static/fulian.jpg&apos;</span>, <span class="hljs-string">&apos;binary&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> error , data </span>) </span>{
        <span class="hljs-keyword">if</span>( error ) <span class="hljs-keyword">throw</span> error 
        res.write( data, <span class="hljs-string">&apos;binary&apos;</span> )
        res.end()
      })
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">default</span>: 
      <span class="hljs-keyword">break</span>
   }

 }).listen( <span class="hljs-number">8000</span>, <span class="hljs-string">&apos;localhost&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
   <span class="hljs-built_in">console</span>.log( <span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x8FD0;&#x884C;&#x5728;&#xFF1A; http://localhost:8000&apos;</span> )
 })
</code></pre>
<h1 id="&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;"><a name="&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;" class="anchor-navigation-ex-anchor" href="#&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;" class="plugin-anchor" href="#&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E94;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x670D;&#x52A1;</h1>
<h3 id="51-readstaticfile"><a name="51-readstaticfile" class="anchor-navigation-ex-anchor" href="#51-readstaticfile"><i class="fa fa-link" aria-hidden="true"></i></a><a name="51-readstaticfile" class="plugin-anchor" href="#51-readstaticfile"><i class="fa fa-link" aria-hidden="true"></i></a>5.1 readStaticFile</h3>
<p>/modules/readStaticFile.js</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5F15;&#x5165;&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;</span>
<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>)
<span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)
<span class="hljs-keyword">var</span> mime = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mime&apos;</span>)

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">readStaticFile</span>(<span class="hljs-params">res, filePathname</span>) </span>{

  <span class="hljs-keyword">var</span> ext = path.parse(filePathname).ext
  <span class="hljs-keyword">var</span> mimeType = mime.getType(ext)

  <span class="hljs-comment">// &#x5224;&#x65AD;&#x8DEF;&#x5F84;&#x662F;&#x5426;&#x6709;&#x540E;&#x7F00;, &#x6709;&#x7684;&#x8BDD;&#x5219;&#x8BF4;&#x660E;&#x5BA2;&#x6237;&#x7AEF;&#x8981;&#x8BF7;&#x6C42;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x6587;&#x4EF6; </span>
  <span class="hljs-keyword">if</span> (ext) {
    <span class="hljs-comment">// &#x6839;&#x636E;&#x4F20;&#x5165;&#x7684;&#x76EE;&#x6807;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x6765;&#x8BFB;&#x53D6;&#x5BF9;&#x5E94;&#x6587;&#x4EF6;</span>
    fs.readFile(filePathname, (err, data) =&gt; {
    <span class="hljs-comment">// &#x9519;&#x8BEF;&#x5904;&#x7406;</span>
      <span class="hljs-keyword">if</span> (err) {
        res.writeHead(<span class="hljs-number">404</span>, { <span class="hljs-string">&quot;Content-Type&quot;</span>: <span class="hljs-string">&quot;text/plain&quot;</span> })
        res.write(<span class="hljs-string">&quot;404 - NOT FOUND&quot;</span>)
        res.end()
      } <span class="hljs-keyword">else</span> {
        res.writeHead(<span class="hljs-number">200</span>, { <span class="hljs-string">&quot;Content-Type&quot;</span>: mimeType })
        res.write(data)
        res.end()
      }
    });
    <span class="hljs-comment">// &#x8FD4;&#x56DE; true &#x8868;&#x793A;, &#x5BA2;&#x6237;&#x7AEF;&#x60F3;&#x8981;&#x7684; &#x662F; &#x9759;&#x6001;&#x6587;&#x4EF6;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// &#x8FD4;&#x56DE; false &#x8868;&#x793A;, &#x5BA2;&#x6237;&#x7AEF;&#x60F3;&#x8981;&#x7684; &#x4E0D;&#x662F; &#x9759;&#x6001;&#x6587;&#x4EF6;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
  }
}

<span class="hljs-comment">// &#x5BFC;&#x51FA;&#x51FD;&#x6570;</span>
<span class="hljs-built_in">module</span>.exports = readStaticFile
</code></pre>
<h3 id="52-server"><a name="52-server" class="anchor-navigation-ex-anchor" href="#52-server"><i class="fa fa-link" aria-hidden="true"></i></a><a name="52-server" class="plugin-anchor" href="#52-server"><i class="fa fa-link" aria-hidden="true"></i></a>5.2 server</h3>
<p>/server.js </p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5F15;&#x5165;&#x76F8;&#x5173;&#x6A21;&#x5757;</span>
<span class="hljs-keyword">var</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">var</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>);
<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">var</span> readStaticFile = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./modules/readStaticFile&apos;</span>);

<span class="hljs-comment">// &#x642D;&#x5EFA; HTTP &#x670D;&#x52A1;&#x5668;</span>
<span class="hljs-keyword">var</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  <span class="hljs-keyword">var</span> urlObj = url.parse(req.url);
  <span class="hljs-keyword">var</span> urlPathname = urlObj.pathname;
  <span class="hljs-keyword">var</span> filePathname = path.join(__dirname, <span class="hljs-string">&quot;/public&quot;</span>, urlPathname);

  <span class="hljs-comment">// &#x8BFB;&#x53D6;&#x9759;&#x6001;&#x6587;&#x4EF6;</span>
  readStaticFile(res, filePathname);
});

<span class="hljs-comment">// &#x5728; 3000 &#x7AEF;&#x53E3;&#x76D1;&#x542C;&#x8BF7;&#x6C42;</span>
server.listen(<span class="hljs-number">3000</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x5668;&#x8FD0;&#x884C;&#x4E2D;.&quot;</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x6B63;&#x5728;&#x76D1;&#x542C; 3000 &#x7AEF;&#x53E3;:&quot;</span>)
})
</code></pre>
<h3 id="53-&#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><a name="53-&#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;" class="anchor-navigation-ex-anchor" href="#53-&#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="53-&#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;" class="plugin-anchor" href="#53-&#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a>5.3 &#x6700;&#x7EC8;&#x76EE;&#x5F55;&#x7ED3;&#x6784;</h3>
<p><img src="../images/dir.jpg" width="300"></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../" class="navigation navigation-prev " aria-label="Previous page: 前言">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="01-Node.js基础.html" class="navigation navigation-next " aria-label="Next page: 01-Node.js基础">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"基础","level":"1.2","depth":1,"next":{"title":"01-Node.js基础","level":"1.2.1","depth":2,"path":"basics/01-Node.js基础.md","ref":"./basics/01-Node.js基础.md","articles":[]},"previous":{"title":"前言","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","github","github-buttons","sharing-plus","anchor-navigation-ex","favicon","insert-logo","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"github":{"url":"https://gitee.com/lurongtao/felixbooks-gp19-Node.js"},"livereload":{},"splitter":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{"shortcut":"./source/images/favicon.jpg","bookmark":"./source/images/favicon.jpg","appleTouch":"./source/images/apple-touch-icon.jpg","appleTouchMore":{"120x120":"./source/images/apple-touch-icon.jpg","180x180":"./source/images/apple-touch-icon.jpg"}},"github-buttons":{"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"insert-logo":{"url":"/felixbooks-gp19-node.js/images/node.js.jpg","style":"background: skyblue; width: 100%; height: 100%;"}},"theme":"default","author":"古艺散人","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"千锋大前端小册-GP19-Node.js","language":"zh-hans","links":{},"gitbook":"3.2.3","description":"Node.js复习"},"file":{"path":"basics/01-Node.js基础.md","mtime":"2020-06-26T01:24:23.857Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-15T09:28:56.165Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

